Python_迭代

迭代定义

  • 通过for...in循环进行的遍历称为迭代

常见迭代对象

  • list
  • tuple
  • dict
  • set
  • string

判断迭代对象

  • 引入包
    from collections import Iterable
  • 执行判断
>>> from collections import Iterable
>>> isinstance('simple',Iterable)
True
>>> isinstance({'key'},Iterable)
True
>>> isinstance({'key':98},Iterable)
True
>>> isinstance((54,77),Iterable)
True
>>> isinstance([54,77],Iterable)
True
>>> isinstance(98,Iterable)
False

利用下标迭代

>>> for i,value in enumerate('string'):
...     print(i,value)
...
0 s
1 t
2 r
3 i
4 n
5 g

双变量迭代

>>> for x,y in [(34,65),(45,45),(8,9),(21,3)]:
...     print(x,y)
...
34 65
45 45
8 9
21 3

你可能感兴趣的:(Python_迭代)