我应该投票关闭它,因为您没有提供足够的调试信息,尤其是没有提供完整的traceback。
但是让我们看看你在做什么。第一次迭代:
In [342]: a==c[0]
Out[342]: array([False, True, False, False, False])
In [343]: np.where(a==c[0])
Out[343]: (array([1]),)
注意where 产生了什么。它是一个元组,而不是一个数字(尽管它只在True 上找到)。如果不清楚,请阅读文档。
当我们尝试将该值分配给b 数组(具有float dtype)的元素时:
In [344]: b[0] = np.where(a==c[0])
TypeError: float() argument must be a string or a number, not 'tuple'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-344-b205dc5b1048>", line 1, in <module>
b[0] = np.where(a==c[0])
ValueError: setting an array element with a sequence.
你收到完整的信息了吗?如果是这样,你为什么只引用最后一行?
虽然可以从 where 元组中提取 [1],但对于其他迭代,结果可能为空 - 不匹配!
In [346]: np.where(a==c[1])
Out[346]: (array([], dtype=int64),)
In [347]: np.where(a==c[2])
Out[347]: (array([3]),)
In [348]: np.where(a==c[3])
Out[348]: (array([], dtype=int64),)
可以将这些where 的结果收集到一个列表中,但为什么呢?
In [349]: [np.nonzero(a==c[i])[0] for i in range(5)]
Out[349]:
[array([1]),
array([], dtype=int64),
array([3]),
array([], dtype=int64),
array([4])]