#coding=utf-8
a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14]
#每行显示6个列表元素
for i,v in enumerate(a):
if i!=0 and i%6==0:
print
print v,
' '*2+'Type:',type(v),'\n'#取得列表数据的类型,换行
结果:
********************List enumerate()********************
The 0 element in list is:1 Type: <type 'int'>
The 1 element in list is:abc Type: <type 'str'>
The 2 element in list is:3 Type: <type 'int'>
The 3 element in list is:I am a boy Type: <type 'str'>
The 4 element in list is:2.34 Type: <type 'float'>
The 5 element in list is:-9 Type: <type 'int'>
********************String enumerate()********************
The 0 element in list is:1 Type: <type 'str'>
The 1 element in list is:a Type: <type 'str'>
The 2 element in list is:b Type: <type 'str'>
The 3 element in list is:3 Type: <type 'str'>
The 4 element in list is:4 Type: <type 'str'>
The 5 element in list is:c Type: <type 'str'>
The 6 element in list is:d Type: <type 'str'>
********************Tuple enumerate()********************
The 0 element in list is:1 Type: <type 'int'>
The 1 element in list is:abc Type: <type 'str'>
The 2 element in list is:2.3 Type: <type 'float'>
The 3 element in list is:5.6 Type: <type 'str'>
The 4 element in list is:I am a boy Type: <type 'str'>
l=range(10)
#方法一
for i in l:
print i,
if i!=0 and i%5==0:
print
print
#方法二
for m in l:
if m!=0 and m%6==0:
print
print m,
有一个list = ["a","b","c","d"],想要转化成一个dict = ["1":"a","2":"b","3":"c","4":"d"]
dict([str(i+1),j for i,j in enumerate(list)])