python object not iterable error in function

我们两清 提交于 2019-12-24 03:53:26

问题


I have a simple function with the following

comdList = range(0,27)
for t, in comdList:
    print t

However it returns a in object not iterable error

outside the function it works fine. Whats going on??


回答1:


Try this:

for t in comdList:
    print t

The extra comma after the t variable was causing the error, because of it Python thinks that the iterable is going to return a sequence of 1-tuples to unpack - for example: ((1,), (2,)) but instead it received an iterable of single elements.



来源:https://stackoverflow.com/questions/17226331/python-object-not-iterable-error-in-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!