python中enumerate用法

 

用于生成二元组。

>>> test1 = ["xxx","yyy","ddd","ccc","aaa"]
>>> enumerate(test1)
<enumerate object at 0x000001C1224ED780>
>>> test2=enumerate(test1)
>>> type(test2)
<class 'enumerate'>
>>> for i in test2:   ## 返回索引和对应元素
    print(i)

    
(0, 'xxx')
(1, 'yyy')
(2, 'ddd')
(3, 'ccc')
(4, 'aaa')
>>> for i in test2:
    print(i)

    
>>> for i in enumerate(test1):
    print(i)

    
(0, 'xxx')
(1, 'yyy')
(2, 'ddd')
(3, 'ccc')
(4, 'aaa')
>>> for i in enumerate(test1):
    print(i)

    
(0, 'xxx')
(1, 'yyy')
(2, 'ddd')
(3, 'ccc')
(4, 'aaa')
>>> for i in enumerate(test1):
    print(i,end="_")

    
(0, 'xxx')_(1, 'yyy')_(2, 'ddd')_(3, 'ccc')_(4, 'aaa')_

 

posted @ 2020-12-26 14:25  小鲨鱼2018  阅读(220)  评论(0编辑  收藏  举报