gpt4 book ai didi

python - 数组列表中存在数组

转载 作者:行者123 更新时间:2023-11-28 22:47:29 25 4
gpt4 key购买 nike

我正在尝试检查数组列表中是否存在数组。我的问题可以归结为以下示例:

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> c = [a,b]
>>> c
[array([1, 2, 3]), array([4, 5, 6])]
>>> a in c
True
>>> np.array([1,2,3]) in c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> d = np.array([1,2,3])
>>> d in c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

如何检查数组列表中是否存在数组?当我使用常规列表时,这正如我所期望的那样工作:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [a,b]
>>> [1,2,3] in c
True
>>> [4,5,6] in c
True
>>> [1,2] in c
False
>>> d = [1,2,3]
>>> d in c
True

最佳答案

简单

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])

c = np.array([a,b]) # < ---- this line!

print repr(c)
print a in c

print np.array([1,2,3]) in c

print [1,2,3] in c # Success!!!
print np.array([1,2,3]).tolist() in c# also success!!!

关于python - 数组列表中存在数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com