【问题标题】:"TypeError: 'int' object is not subscriptable" - Am I using queue wrong?“TypeError: 'int' object is not subscriptable” - 我使用队列错误吗?
【发布时间】:2019-01-12 17:30:17
【问题描述】:

我正在编写我昨天在这里询问过的代码:how to bypass 'dictionary changed size during iteration'

按照建议,我开始使用 BFS 双端队列,但是现在,当我尝试输入单元格详细信息时,它可以工作(我可以打印这些值并且它们会正确显示)但由于某种原因编译器仍然告诉我“TypeError:'int'对象不可下标”。我在这里查看了有关此问题的其他一些文章,他们都在谈论尝试到达 int 内的位置,因为它是一个数组,但它不是我所做的,我正在尝试到达一个元组。 (我检查了单元格的类型 - 它是一个元组)

如果有任何帮助,将不胜感激。

    for row in range(len(node.state)):
        for col in range(len(node.state[0])):
            if node.state[row][col] == DEST or node.state[row][col] == PDEST or node.state[row][col] == BDEST:
                visitedCells[row, col] = 0

    queue = collections.deque(visitedCells.items())

    while queue:
        cell, val = queue.pop()

        row = cell[0]
        col = cell[1]

        if ((row + 1, col) not in visitedCells and (node.state[row + 1][col] == EMPTY or node.state[row + 1][col]
                                                    == BOX or node.state[row + 1][col] == PLAYER or
                                                    node.state[row + 1][col] == ICE or node.state[row + 1][col]
                                                    == PICE or node.state[row + 1][col] == BICE)):
            visitedCells[row + 1, col] = val + 1
            queue.append((row + 1, col))

回溯是:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\roniz\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:/Users/roniz/PycharmProjects/AIp1t2/check.py", line 20, in run
    self.result = func(*args, **kwargs)
  File "C:/Users/roniz/PycharmProjects/AIp1t2/check.py", line 64, in <lambda>
    result = check_problem(p, (lambda p: search.best_first_graph_search(p, p.h)), timeout)
  File "C:\Users\roniz\PycharmProjects\AIp1t2\search.py", line 257, in best_first_graph_search
    frontier.append(node)
  File "C:\Users\roniz\PycharmProjects\AIp1t2\utils.py", line 750, in append
    bisect.insort(self.A, (self.f(item), item))
  File "C:\Users\roniz\PycharmProjects\AIp1t2\utils.py", line 361, in memoized_fn
    val = fn(obj, *args)
  File "C:\Users\roniz\PycharmProjects\AIp1t2\ex1.py", line 128, in h
    row = cell[0]
TypeError: 'int' object is not subscriptable

【问题讨论】:

  • 您在此处附加一个新元组:queue.append((row + 1, col))缺少一个值,您的顶部字典项是带有 嵌套((row, col), value) 元组> 元组。你从队列中弹出cell, val,然后索引cell,所以对于上面的追加,cellrow + 1colvalrow + 1 不是元组。
  • 而 python 通常非常擅长捕捉这样的错误。你很可能会在某处得到some_int[some_index]
  • @MartijnPieters - 已修复,谢谢 - 添加回溯

标签: python collections queue tuples heuristics


【解决方案1】:

您以((row, cell), value) 元组开始您的队列:

queue = collections.deque(visitedCells.items())

但在处理时仅将 (row, cell) 元组附加到队列中:

queue.append((row + 1, col))

这意味着该行

cell, val = queue.pop()

只给出了两个整数,分别是cellval。您还需要附加该值:

queue.append(((row + 1, col), val + 1))

或更改您的队列以仅存储 (row, col) 元组并从您的 visitedCells 字典中获取值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    相关资源
    最近更新 更多