首页 > 编程 > Python > 正文

python判断一个对象是否可迭代

2019-11-08 18:50:57
字体:
来源:转载
供稿:网友

如何判断一个对象是可迭代对象? 方法是通过collections模块的Iterable类型判断:

>>> from collections import Iterable>>> isinstance('abc',Iterable)True>>> isinstance([1,2,3,4],Iterable)True>>> isinstance(1234,Iterable)False>>> isinstance((1,),Iterable)True>>> L = ['a','b','c']>>> enumerate(L)<enumerate object at 0x03AA94E0>>>> isinstance(enumerate(L),Iterable)True>>> for m,n in enumerate(L):... PRint m,n ... 0 a1 b2 c
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表