center 是 numpy 数组的列表 [ ]。Shortest_dist[1] 是一个 numpy 数组。但是,当我这样做时:
centers.index(shortest_dist[1])
Run Code Online (Sandbox Code Playgroud)
它告诉我
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
这很奇怪,所以我尝试了以下方法:
请参阅以下演示。我无法理解正在发生的事情。
>>>
>>>
>>>
>>> a = np.asarray([1,2,3,4,5])
>>> b = np.asarray([2,3,4,5,6])
>>> c = []
>>> c.append(a)
>>> c.append(b)
>>> c.index(a)
0
>>> c.index(c[0])
0
>>> c.index(c[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more …Run Code Online (Sandbox Code Playgroud) 假设我定义了以下变量:
mode = "access"
allowed_modes = ["access", "read", "write"]
Run Code Online (Sandbox Code Playgroud)
我目前有一个类型检查语句
assert any(mode == allowed_mode for allowed_mode in allowed_modes)
Run Code Online (Sandbox Code Playgroud)
但是,似乎我可以简单地替换它
assert mode in allowed_modes
Run Code Online (Sandbox Code Playgroud)
根据ThiefMaster在Python List Class __contains__ Method Functionality中的回答,这两个应该是等价的.确实如此吗?我怎样才能通过查找Python的源代码轻松验证这一点?