gpt4 book ai didi

python - 当元素包含 numpy 数组时,无法测试 python 列表元素成员资格

转载 作者:行者123 更新时间:2023-12-03 18:48:51 25 4
gpt4 key购买 nike

我使用 namedtuple 定义了一个自定义数据结构,其中包含 numpy 数组。

from collections import namedtuple
import numpy as np

AA = namedtuple('AA', 'name, x')
c = []
c.append(AA('x', np.arange(3)))
c.append(AA('x', np.arange(3)))
c.append(AA('y', np.arange(3)))
c[0] in c
c[1] in c
c[2] in c
奇怪的是, c[1]测试失败,而其他两个有效。
ValueError                                Traceback (most recent call last)
<ipython-input-70-c1daf83cd082> in <module>
----> 1 c[1] in c

ValueError: The truth value of an array with more than one element is ambiguous. Use c.any() or c.all()
该错误似乎与 numpy arrray 相等测试有关 c[0].x == c[1].x .但由于某种原因,第一个元素的成员资格测试总是成功
s = []
s.append(np.arange(3))
s.append(np.arange(3))
s[0] in s
s[1] in s
另请参阅此示例
class A:
def __eq__(self, other):
raise ValueError

a = [A(), A()]
a[0] in a
a[1] in a
我也不知道为什么 c[2] in c返回 True .

最佳答案

简化:

def my_in(obj, container):
for elem in container:
if bool(my_compare_equal(obj, elem)):
return True
return False

def my_compare_equal(obj, elem):
if id(obj) == id(elem):
return True
return obj.__eq__(elem)
更多数据,见 list_contains 然后 PyObject_RichCompareBool , PyObject_RichCompare , do_richcompare
“伪执行”步骤:
c = [AA('x', np.arange(3)),
AA('x', np.arange(3)),
AA('y', np.arange(3))]

c[0] in c
# explain:
my_in(obj=c[0], container=c)
# for loop:

# 0-iteration:
# elem = c[0]
my_compare_equal(obj=c[0], elem=c[0])
# (id(c[0]) == id(c[0])) == True
# --> True
bool(True)
# True.__bool__()
# --> True
# --> True

c[1] in c
# explain:
my_in(obj=c[1], container=c)
# for loop:

# 0-iteration:
# elem = c[0]
my_compare_equal(obj=c[1], elem=c[0])
# (id(c[1]) == id(c[0])) == False
# c[1].__eq__(c[0])
# compare tuples element by element:
# 0-iteration:
my_compare_equal('x', 'x') == True
# 1-iteration:
my_compare_equal(np.arange(3), np.arange(3))
# (id(np.arange(3)) == id(np.arange(3))) == False
# np.arange(3).__eq__(np.arange(3))
# --> np.ndarray([True, True, True])
bool(np.ndarray([True, True, True]))
# np.ndarray([True, True, True]).__bool__()
raise ValueError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")

c[2] in c
# explain:
my_in(obj=c[2], container=c)
# for loop:

# 0-iteration:
# elem = c[0]
my_compare_equal(obj=c[2], elem=c[0])
# (id(c[2]) == id(c[0])) == False
# c[2].__eq__(c[0])
# compare tuples element by element:
# 0-iteration:
my_compare_equal('y', 'x') == False
# --> False
# --> False

# 1-iteration:
# analogiusly as 0-iteration:
my_compare_equal(obj=c[2], elem=c[1])
# --> False

# 2-iteration:
my_compare_equal(obj=c[2], elem=c[2])
# (id(c[2]) == id(c[2])) == True
# --> True
# --> True

关于python - 当元素包含 numpy 数组时,无法测试 python 列表元素成员资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67271766/

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