zip()
- 将两到多个可迭代对象中,具有对应关系的元素打包,好处是节省内存
a = [1, 2, 3]
b = [4, 5, 6, 7]
print(zip(a, b))
# [(1,4),(2,5),(3,6)] 以较短的为新list长度
- zip(*)可以理解为解压,可用于二维矩阵的行列互换,速度快
a, b = zip(*[(1,4),(2,5),(3,6)])
print(a, b)
# a = (1, 2, 3) b = (4, 5, 6)
c = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(zip(*c)))
# [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
网友评论