如何判断一个对象是可迭代对象? 
方法是通过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 a
1 b
2 c

 

相关文章:

  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2021-11-27
  • 2021-12-07
猜你喜欢
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-11-06
相关资源
相似解决方案