zip(*iteables)
zip函数可以接受一系列的可迭代对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后由这些tuple(元组)组成一个list(列表)返回。
a = [1,2,3,4,5]
b = ['a','b','c','d','e']
c = zip(a,b)
print(type(c)) #输出:<class 'zip'>
print(c) #输出:<zip object at 0x000001D32F87EFC0>
print(list(c)) #输出:[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
如果传入的可迭代对象的长度不一致,则返回可迭代对象中最短的一个对象的长度。
a = [1,2,3,4,5]
b = ['a','b','c','d']
c = zip(a,b)
print(list(c)) #输出:[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
names = ['zhangsan','lisi','wangwu','zhangliu','sunqi']
weights = [72,68,72,66,76]
user_weight = dict(zip(names,weights))
#输出:{'zhangsan': 72, 'lisi': 68, 'wangwu': 72, 'zhangliu': 66, 'sunqi': 76}
print(user_weight)