Python3-笔记-C-002-函数-zip

def testzip():
# zip是将每个可迭代对象的对应位置元素打包成一个tuple
# 操作符*zip函数配合可以实现与zip相反的功能, 即将合并的序列拆成多个tuple
# 新的序列的长度以参数中最短的序列为准
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = list(zip(x, y, z)) # <class 'list'>: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
x = [1, 2, 3]
y = [4, 5, 6, 7]
xy = list(zip(x, y)) # <class 'list'>: [(1, 4), (2, 5), (3, 6)]
x = [1, 2, 3]
x = list(zip(x)) # <class 'list'>: [(1,), (2,), (3,)]
x = list(zip()) # <class 'list'>: []
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = list(zip(x, y, z)) # <class 'list'>: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
u = list(zip(*xyz)) # <class 'list'>: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
x = [1, 2, 3]
r = list(zip(*[x] * 3)) # <class 'list'>: [(1, 1, 1), (2, 2, 2), (3, 3, 3)]

# 使用zip反转字典
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
m2 = dict(zip(m.values(), m.keys()))
原文地址:https://www.cnblogs.com/vito13/p/7730019.html