python enumerate用法,pythonenumerate,enumerate是py


enumerate是python的内置函数,使用这个函数枚举列表会有更加直接,更可读。

#!/usr/bin/env python# coding:utf-8#enumeratec = ['a', 'b', 'c', 'd']for (index, item) in enumerate(c):    print index, itemname = ['lk', 'sun', 'maidou']city = ['qd', 'qs', 'dg']d = dict(zip(name, city))print d#open filewith open('d:/test/ToPdf.py') as fp:    for line in fp:        print line,#mapa = [1, 2, 3, 4]b = map(lambda x: x+1, a)print b#filter, c = filter(lambda x: x%2==0, a)print cprint "sum", reduce(lambda x, y: x+y, a)

评论关闭