TypeError:'int'对象在没有迭代存在时不可迭代吗? [英] TypeError: 'int' object is not iterable while no iteration exists?

查看:222
本文介绍了TypeError:'int'对象在没有迭代存在时不可迭代吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个BFS,而Python给了我一个int对象一个不可迭代的错误.

I am trying to do a BFS while Python gives me an int object not iterable error.

部分代码是

visited, queue = set(), collections.deque( ((0, 0), [ (0, 0) ] ) ) 
# tuple: current location,  path
while queue: 
    vertex = queue.popleft()
    i,j=vertex[0]
    if i+1<=dim-1 and (i+1, j) not in visited and X[i+1, j]==0:
        visited.add(( i+1, j) )
        temp=( (i+1, j), vertex[1]+[(i+1, j)])
        if temp[0]==(dim-1, dim-1):
            return True, temp[1]
        queue.append(temp)

在while循环下,我完全在进行其他任何迭代!

Under the while loop, I am doing any other iteration at all!

推荐答案

i, j = <expression>表示该方法是可迭代的,并且具有两个元素.而在您的情况下,表达式(vertex [0])会产生一个不可迭代的整数.

i, j = <expression> implies that is iterable and has exactly two elements. Whereas in your case, the expression (vertex[0]) yields an integer, which is not iterable.

语句:i, j = vertex[0]大致等同于:

iterator = iter(vertex[0])
i = next(iterator) # this is iteration, but outside of a loop
j = next(iterator)
try:
    next(iterator)
except StopIteration:
    pass
else:
    raise ValueError

您的代码应为

i = vertex[0]
j = vertex[1]

i, j = vertex

这篇关于TypeError:'int'对象在没有迭代存在时不可迭代吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆