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对象而不是可迭代的错误
在功能之外,它工作正常.这是怎么回事??
试试这个:
for t in comdList:
print t
Run Code Online (Sandbox Code Playgroud)
t变量之后的额外逗号导致错误,因为Python认为iterable将返回一个1元组的序列来解包 - 例如:((1,), (2,))但它接收了一个可迭代的单个元素.