nJcx's Blog

生而不忧,死而不怖。得其时横刀天下,不得其时蓬头而行。

Python常用函数-zip


作用

  • 并列遍历
In [1]: name=['jim','tom','lili']

In [2]: age=[20,30,40]

In [3]: tel=['133','156','189']

In [4]: zip(name,age,tel)
Out[4]: [('jim', 20, '133'), ('tom', 30, '156'), ('lili', 40, '189')]

In [5]: zz = zip(name,age,tel)

In [6]: zip(*zz)
Out[6]: [('jim', 'tom', 'lili'), (20, 30, 40), ('133', '156', '189')]

使用场景

  • 二维矩阵变换(矩阵的行列互换)