python list遍歴方法の概要

6742 ワード

list=['a','b','c','d','e']
#  1:
print('#  1:')
#i     item,list    ,  i       
for i in list:
    #list.index(i) + 1    1  ,   0  
    print("  :%s  :%s" % (list.index(i) + 1, i))
#  2:
print('#  2:')
#i       ,  len(list)     
for i in range(len(list)):
    #  i   ,       ,+ 1    1  ,   0  
    print("  :%s  :%s" % (i + 1, list[i]))
#  3
print('#  3:')
#i     ,item      ,enumerate(  ) python    
for i,item in enumerate(list):
    print("  :%s  :%s" % (i + 1, item))
#enumerate        ,       ,      start  
#enumerate(sequence, [start=0]),sequence        ,     ,  ,    ,start       
#
print('enumerate    :')
str='AASSFFS'
tuple=('AAA','BBBB','CCC')
dict={'a':'111','b':'222'}
print('enumerate    :   ')
for i,item in enumerate(str):
    print("  :%s  :%s" % (i + 1, item))
print('enumerate    :  ')
for i,item in enumerate(tuple):
    print("  :%s  :%s" % (i + 1, item))
print('enumerate    :  ')
#      key
for i,item in enumerate(dict):
    print("  :%s  :%s" % (i + 1, item))