【发布时间】:2011-11-06 00:15:04
【问题描述】:
看来我还在为the "in" operator in numpy 挣扎。情况如下:
>>> a = np.random.randint(1, 10, (2, 2, 3))
>>> a
array([[[9, 8, 8],
[4, 9, 1]],
[[6, 6, 3],
[9, 3, 5]]])
我想获取第二个元素在(6, 8) 中的那些三元组的索引。我凭直觉尝试的方式是:
>>> a[:, :, 1] in (6, 8)
ValueError: The truth value of an array with more than one element...
我的最终目标是在这些位置插入前面的数字乘以 2。 使用上面的示例,a 应该变为:
array([[[9, 18, 8], #8 @ pos #2 --> replaced by 9 @ pos #1 by 2
[4, 9, 1]],
[[6, 12, 3], #6 @ pos #2 --> replaced by 6 @ pos #1 by 2
[9, 3, 5]]])
提前感谢您的建议和时间!
【问题讨论】:
标签: python numpy python-2.7