【发布时间】:2013-06-18 01:24:56
【问题描述】:
我有一个简单的函数如下
comdList = range(0,27)
for t, in comdList:
print t
但是它返回一个 in object not iterable 错误
在函数之外它工作正常。怎么回事??
【问题讨论】:
标签: python python-2.7 for-loop iterable
我有一个简单的函数如下
comdList = range(0,27)
for t, in comdList:
print t
但是它返回一个 in object not iterable 错误
在函数之外它工作正常。怎么回事??
【问题讨论】:
标签: python python-2.7 for-loop iterable
试试这个:
for t in comdList:
print t
t 变量之后的额外逗号导致了错误,因为它 Python 认为可迭代对象将返回要解包的 1 元组序列 - 例如:((1,), (2,)) 但它收到了一个可迭代对象单个元素。
【讨论】:
for t, in comdList 仅在 comdList 是 1 元组序列 ((1,), (2,), ...) 而非高维元组序列时才有效。