ValueError: The truth value of an array… from dict key comparison

非 Y 不嫁゛ 提交于 2019-12-13 03:28:51

问题


I can't figure out why I keep getting this error:

        if node not in self.children: return path    
    ValueError: The truth value of an array with more than one element is 
    ambiguous. Use a.any() or a.all()

    def _select(self, node):

    "Find an unexplored descendent of `node`"
    path = []
    while True:
        path.append(node)
        breakpoint()
        if node not in self.children: return path
        if node.is_terminal(): return path

self.children is a dict() of set() of Nodes. node is obviously a Node

I get the same error in debugger when I try:


    (Pdb) self.children[node]
    *** ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm wondering if it has something to do with these, but I can't tell:


def __hash__(self):
    "Nodes must be hashable"
    return hash(tuple(self.board.flatten()))

def __eq__(node1, node2):
    "Nodes must be comparable"
    #if node1 is None: return True
    #if node2 is None: return True
    return node1.board == node2.board

self.children has only one key:

    (Pdb) self.children.keys()

    turn:1
        |   |   |   |   |   |   |   |
        |   |   |   |   |   |   |   |
        |   |   |   |   |   |   |   |
        |   |   |   |   |   |   |   |
        |   |   |   |   |   |   |   |
        | O |   |   |   |   |   |   |
          _   _   _   _   _   _   _
          0   1   2   3   4   5   6
    dict_keys([
    turn: 1,last: (5, 0), done False, winner: None])

and a lot of set() values

    Pdb) self.children
    {
    turn: 1,last: (5, 0), done False, winner: None: {
    turn: 2,last: (5, 2), done False, winner: None,
    turn: 2,last: (5, 3), done False, winner: None,
    turn: 2,last: (5, 6), done False, winner: None,
    turn: 2,last: (5, 1), done False, winner: None,
    turn: 2,last: (4, 0), done False, winner: None,
    turn: 2,last: (5, 4), done False, winner: None,
    turn: 2,last: (5, 5), done False, winner: None}}

but this code works just fine:


    from random import randint
    children = dict()  # children of each node
    j = set()
    for i in range(10):
       j = (randint(0,5) for _ in range(5))
       children[i] = j
    print(12 not in children)

    output: true

any ideas why I can't write if node not in self.children: return path ?

来源:https://stackoverflow.com/questions/57541777/valueerror-the-truth-value-of-an-array-from-dict-key-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!