python对象不能迭代错误的功能 [英] python object not iterable error in function

查看:141
本文介绍了python对象不能迭代错误的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的函数如下:

I have a simple function with the following

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

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

However it returns a in object not iterable error

外部的函数工作正常。怎么回事?

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

推荐答案

试试这个:

Try this:

for t in comdList:
    print t

额外的逗号在 t 变量导致错误之后,因为Python认为iterable将返回一个1-tuples序列来解压缩,例如:((1,),(2,)),但是它收到了一个单一元素的迭代。

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.

这篇关于python对象不能迭代错误的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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