python对象在函数中没有可迭代的错误

Dar*_*ran 2 python for-loop iterable python-2.7

我有一个简单的功能如下

comdList = range(0,27)
for t, in comdList:
    print t
Run Code Online (Sandbox Code Playgroud)

但是它返回一个in对象而不是可迭代的错误

在功能之外,它工作正常.这是怎么回事??

Ósc*_*pez 6

试试这个:

for t in comdList:
    print t
Run Code Online (Sandbox Code Playgroud)

t变量之后的额外逗号导致错误,因为Python认为iterable将返回一个1元组的序列来解包 - 例如:((1,), (2,))但它接收了一个可迭代的单个元素.

  • @Darran,欢迎你!请不要忘记[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)这个或任何其他有用的答案,只需点击选中左侧的标记;) (2认同)