Python 中遍歷序列中元素和下標


enumerate 函數用於遍歷序列中的元素以及它們的下標

for  i,v in  enumerate(['tic','tac','toe']):
    print  i,v
    
#0 tic
#1 tac
#2 toe

for i,j in enumerate(('a','b','c')):
    print i,j
    
#0 a
#1 b
#2 c

for i,j in enumerate({'a':1,'b':2}):
    print i,j

#0 a
#1 b

 遍歷字典的key和value

knights={'galahad':'the pure','robin':'the brave'}
for  k,v  in  knights.iteritems():
    print  k,v 

#galahad the pure
#robin the brave

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM