标签:int value 接受 取数 运行 [] 多个 example val
zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。
zip函数在获取数据后,生成字典(dict)时比较好用。
1 # Code based on Python 3.x 2 # _*_ coding: utf-8 _*_ 3 # __Author: "LEMON" 4 5 pList = [(‘li‘, ‘LY‘, 80), (‘zeng‘, ‘ZW‘, 90), (‘dudu‘, ‘LR‘, 98)] 6 names = [] 7 scores = [] 8 for i in range(len(pList)): 9 aStr = pList[i][0] 10 bStr = pList[i][2] 11 names.append(aStr) 12 scores.append(bStr) 13 14 15 aDict = dict(zip(names, scores)) 16 print(aDict)
运行结果:
1 {‘dudu‘: 98, ‘zeng‘: 90, ‘li‘: 80}
当然,上述案例有更简单的实现方法:
1 # Code based on Python 3.x 2 # _*_ coding: utf-8 _*_ 3 # __Author: "LEMON" 4 5 pList = [(‘li‘, ‘LY‘, 80), (‘zeng‘, ‘ZW‘, 90), (‘dudu‘, ‘LR‘, 98)] 6 aDict = {} 7 for data in pList: 8 aDict[data[0]] = data[2] 9 # dict[Key]=value 10 print(aDict)
标签:int value 接受 取数 运行 [] 多个 example val
原文地址:http://www.cnblogs.com/lemonbit/p/6238477.html