python 列表推导

废话不多说,直接上代码

 

#coding=utf-8

def getitem(index, element):
return '%d: %s' % (index, element)

def getitem2():
array = ['I', 'love', 'Python']
arrayIndex = [getitem(index, element) for index, element in enumerate(array)]
return arrayIndex

if __name__=="__main__":
a=getitem2()
print(a)


运行结果如下图:

F:\dev\python\python.exe F:/pyCharm/pratice/list_demo/demo_1.py
['0: I', '1: love', '2: Python']

Process finished with exit code 0

posted @ 2019-03-07 19:11  志不坚者智不达  阅读(201)  评论(0编辑  收藏  举报