python中序列的内建函数enumerate及zip

 

001、enumerate:

>>> a = ["xx", "aa", "tt", "cc"]
>>> a
['xx', 'aa', 'tt', 'cc']
>>> for i in enumerate(a):        ## enumerate返回可迭代对象的索引值,生成二元组
...     print(i)
...
(0, 'xx')
(1, 'aa')
(2, 'tt')
(3, 'cc')
>>> enumerate(a)         ## 返回可迭代对象
<enumerate object at 0x7f6ec6b38750>

 

 

002、zip

>>> a = ["xx", "kk", "jj", "ee"]
>>> b = [111, 555, 333, 444]
>>> c = (873, 343, 344, 888)
>>> a
['xx', 'kk', 'jj', 'ee']
>>> b
[111, 555, 333, 444]
>>> c
(873, 343, 344, 888)
>>> for i in zip(a, b, c):       ## 生成元组
...     print(i)
...
('xx', 111, 873)
('kk', 555, 343)
('jj', 333, 344)
('ee', 444, 888)
>>> zip(a, b, c)               ## 返回可迭代对象
<zip object at 0x7f6ec6436a08>

 

posted @ 2022-06-05 23:39  小鲨鱼2018  阅读(34)  评论(0编辑  收藏  举报