【问题标题】:python simple example - ValueError: setting an array element with a sequencepython 简单示例 - ValueError:使用序列设置数组元素
【发布时间】:2022-01-20 09:24:25
【问题描述】:

当我执行这个 python 代码时,我收到一条错误消息:ValueError: setting an array element with a sequence。

有什么问题?你能帮帮我吗?

a=np.array([1,3,5,7,9], dtype=int)
c=np.array([3,4,7,8,9], dtype=int)
b=np.zeros(len(a))

for i in range(len(a)):
    b[i]= np.where(a == int(c[i]))

【问题讨论】:

  • 你想达到什么目的?
  • 你看where函数的结果了吗?

标签: python numpy


【解决方案1】:

问题在于b[i]= np.where(a == int(c[i]))this 在您的示例中返回一个仅包含单个元素的数组。但是, v 也是一维的,您必须分配标量而不是数组。如果您确定搜索只返回 1 个元素,则可以执行 b[i]= np.where(a == int(c[i]))[0]

【讨论】:

  • 感谢您的回复。这是我想要的。首先,我将b[i]= np.where(a == int(c[i]))[0] 分解为d=np.where(a == int(c[i]))b[i]=d,然后它起作用了。无论如何,再次感谢您!
【解决方案2】:

您也许可以尝试将其附加到 b 而不是替换值?

     B.append(np.where( a == c[i]))

还可以尝试在 for 循环中使用 if 条件:)

【讨论】:

  • 我也推荐这个解决方案,但你应该声明b 现在是一个列表而不是一个数组
【解决方案3】:

问题在于您的数组b。由于是数组,所以不如列表灵活。由于您的 np.where 语句有时会返回一个空数组,甚至可能返回一个包含更多标量的数组,所以它有点问题。 您最好将b 定义为列表:

a=np.array([1,3,5,7,9], dtype=int)
c=np.array([3,4,7,8,9], dtype=int)
b=[]
for i in range(len(a)):
     b.append(np.where(a==c[i])[0])

print(b)

[array([1]),
 array([], dtype=int64),
 array([3]),
 array([], dtype=int64),
 array([4])]

只有当您完全确定您的np.where 语句将返回一个且只有一个标量时,您才能使用@Simon Hawe 的解决方案。

请注意,这将返回声明为 True 的 a 的索引!如果你想要值本身,代码就变成了

a=np.array([1,3,5,7,9], dtype=int)
c=np.array([3,4,7,8,9], dtype=int)
b=[]
for i in range(len(a)):
    b.append(a[np.where(a==c[i])])

print(b)

[array([3]),
 array([], dtype=int64),
 array([7]),
 array([], dtype=int64),
 array([9])]


【讨论】:

  • 感谢您的回复。这不完全是我想要的,但它会对我有所帮助。太感谢了。 :-)
【解决方案4】:

我应该投票关闭它,因为您没有提供足够的调试信息,尤其是没有提供完整的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])]

【讨论】:

  • --------------------------------------------------------------------------- ValueError Traceback (most recent call last) &lt;ipython-input-606-7b50beb044f5&gt; in &lt;module&gt;() 14 15 for i in range(len(a)): ---&gt; 16 b[i]= np.where(a == int(c[i])) ValueError: setting an array element with a sequence.
  • 以上是我收到的全部错误消息。并感谢您的回复。这对我很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 2011-06-08
  • 2018-08-04
  • 2019-08-04
相关资源
最近更新 更多