我测试了这段代码:
dic = {1:[[[1],2],[[2],3],[[3],4]],2:[[[5],6],[[7],8]],3:[[[9],10],[[11],12]]}
klst = list(dic.keys())
# print(klst)
print(dic[1])
for item in dic[klst[0]]:
if item == [[2],3]:
dic[klst[0]].remove(item)
print(item)
break
print(dic)
我得到以下输出:
[[[1], 2], [[2], 3], [[3], 4]]
[[2], 3]
{1: [[[1], 2], [[3], 4]], 2: [[[5], 6], [[7], 8]], 3: [[[9], 10], [[11], 12]]}
因此,remove() 完美地从字典的值列表中删除了该列表。
我已经实现了类似的结构,其中键是启发式值,值是列表的列表。内部列表由一个 numpy 数组和一个字符串组成。例如:
[array([[10, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 0, 9, 11]]), 'left']
现在我试图从字典中删除这样的列表并获取
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
该代码前两次有效,从字典中删除此类列表
for move in index_move_priority:
for item in open_nodes[keylist[0]]: # item in list of states
if move == item[1]:
print("KLYST",keylist[0])
print("////////////////",open_nodes[keylist[0]])
print("ITEM->",item)
print(type(open_nodes[keylist[0]]))
print(type(item))
open_nodes[keylist[0]].remove(item)
find_neighbor_node = item
temp = True
break
if temp == True:
break
这里open_nodes是我的字典。该行
open_nodes[keylist[0]].remove(item)
导致 ValueError。在出现错误之前,我已经打印了 keylist[0] 以及 open_nodes[keylist[0]] 的内容,如下所示。
KLYST 8
//////////////// [[array([[10, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 0, 9, 11]]), 'left'], [array([[10, 1, 2, 3],
[ 4, 0, 6, 7],
[ 8, 9, 5, 11]]), 'up-left'], [array([[10, 1, 0, 3],
[ 4, 5, 2, 7],
[ 8, 9, 6, 11]]), 'up'], [array([[10, 1, 2, 3],
[ 4, 0, 5, 7],
[ 8, 9, 6, 11]]), 'left'], [array([[10, 0, 2, 3],
[ 4, 5, 1, 7],
[ 8, 9, 6, 11]]), 'up-left']]
ITEM-> [array([[10, 1, 0, 3],
[ 4, 5, 2, 7],
[ 8, 9, 6, 11]]), 'up']
我不明白为什么会收到不明确的值错误。 更新:在此处查找代码 https://github.com/idk-kid/test/blob/master/test.ipynb
最佳答案
remove
使用相等测试。对数组的这种测试与列表不同。
列表删除:
In [86]: alist = [[1,2],[3,4]]
In [87]: alist.remove([1,2])
In [88]: alist
Out[88]: [[3, 4]]
数组删除 - 你的错误
In [89]: alist = [np.array([1,2]),np.array([3,4])]
In [90]: alist.remove(np.array([1,2]))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-90-367c77859b46> in <module>()
----> 1 alist.remove(np.array([1,2]))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果我按位置引用数组,则 remove
有效。比较第一个测试ID:
In [91]: alist.remove(alist[0])
In [92]: alist
Out[92]: [array([3, 4])]
但是如果 id 不匹配,则会继续测试值。但是对数组进行 bool 测试会生成 bool 数组:
In [93]: alist[0]==np.array([3,4])
Out[93]: array([ True, True])
在需要标量(例如 if
子句)的上下文中使用多元素数组会产生此歧义错误。
可以按值进行列表比较,返回标量:
In [94]: Out[88][0]==[3,4]
Out[94]: True
列表和数组在很多方面都很相似,但通常不能互相替代。
编辑
当数组嵌套在列表中时,同样的情况也适用。使用您最后打印的照片:
In [104]: alist.remove(item)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-104-f439b16b219c> in <module>()
----> 1 alist.remove(item)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [105]: alist[0]
Out[105]:
[array([[10, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 0, 9, 11]]), 'left']
In [106]: item
Out[106]:
[array([[10, 1, 0, 3],
[ 4, 5, 2, 7],
[ 8, 9, 6, 11]]), 'up']
In [107]: alist[0]==item
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-107-d1fe30e1a331> in <module>()
----> 1 alist[0]==item
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [108]: alist.remove(alist[0])
In [109]: alist
Out[109]:
[[array([[10, 1, 2, 3],
[ 4, 0, 6, 7],
[ 8, 9, 5, 11]]), 'up-left'], [array([[10, 1, 0, 3],
[ 4, 5, 2, 7],
[ 8, 9, 6, 11]]), 'up'], [array([[10, 1, 2, 3],
[ 4, 0, 5, 7],
[ 8, 9, 6, 11]]), 'left'], [array([[10, 0, 2, 3],
[ 4, 5, 1, 7],
[ 8, 9, 6, 11]]), 'up-left']]
如果列表 ID 匹配,则可以删除。但如果不这样做,它就会逐个元素地比较列表。但是,当这些元素之一是数组时,就会遇到歧义问题。
关于python - 从作为字典值的列表中删除值时,ValueError 真值不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52430910/