zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。
zip函数在获取数据后,生成字典(dict)时比较好用。
for examples:
# Code based on Python 3.x
# _*_ coding: utf-8 _*_
# __Author: "LEMON" pList = [('li', 'LY', 80), ('zeng', 'ZW', 90), ('dudu', 'LR', 98)]
names = []
scores = []
for i in range(len(pList)):
aStr = pList[i][0]
bStr = pList[i][2]
names.append(aStr)
scores.append(bStr) aDict = dict(zip(names, scores))
print(aDict)
运行结果:
{'dudu': 98, 'zeng': 90, 'li': 80}
当然,上述案例有更简单的实现方法:
# Code based on Python 3.x
# _*_ coding: utf-8 _*_
# __Author: "LEMON" pList = [('li', 'LY', 80), ('zeng', 'ZW', 90), ('dudu', 'LR', 98)]
aDict = {}
for data in pList:
aDict[data[0]] = data[2]
# dict[Key]=value
print(aDict)