python(29)强大的zip函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python(29)强大的zip函数相关的知识,希望对你有一定的参考价值。

zip函数:它是python的内建函数,(与序列有关的内建函数有:sorted()、reversed()、enumerate()、zip()),其中sorted()和zip()返回一个序列(列表)对象,reversed()、enumerate()返回一个迭代器(类似序列)

代码一:

>>> name = ("lilei","lihua","zhang")
>>> age = ("21","14","34")
>>> for a,n in zip(name,age):
...     print a,n
...
lilei 21
lihua 14
zhang 34
>>>

代码二:

>>> all={"hell":20,"helo":23,"sony":15,"lei":19}
>>> for i in all.keys():
...     print i,all[i]
...
hell 20
helo 23
lei 19
sony 15
>>>

代码三:若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。

>>> aa = ("1","3","5")
>>> bb = ("ee","tt")
>>> for a,n in zip(aa,bb):
...     print a,n
...
1 ee
3 tt
>>> zip(aa,bb)
[(1, ee), (3, tt)]
>>>

代码四:zip()配合*号操作符,可以将已经zip过的列表对象解压

>>> aa
(1, 3, 5)
>>> bb
(ee, tt)
>>> ad = zip(aa,bb)
>>> zip(*ad)
[(1, 3), (ee, tt)]
>>> zip(ad)
[((1, ee),), ((3, tt),)]
>>>

 参考地址:http://www.cnblogs.com/BeginMan/archive/2013/03/14/2959447.html

扩宽:

* 二维矩阵变换(矩阵的行列互换)
比如我们有一个由列表描述的二维矩阵
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
通过python列表推导的方法,我们也能轻易完成这个任务
print [ [row[col] for row in a] for col in range(len(a[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
另外一种让人困惑的方法就是利用zip函数:
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zip(*a)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> map(list,zip(*a))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
 
zip函数接受任意多个序列作为参数,将所有序列按相同的索引组合成一个元素是各个序列合并成的tuple的新序列,新的序列的长度以参数中最短的序列为准。另外(*)操作符与zip函数配合可以实现与zip相反的功能,即将合并的序列拆成多个tuple。
①tuple的新序列
>>>>x=[1,2,3],y=[a,b,c]
>>>zip(x,y)
[(1,a),(2,b),(3,c)]

②新的序列的长度以参数中最短的序列为准.
>>>>x=[1,2],y=[a,b,c]
>>>zip(x,y)
[(1,a),(2,b)]

③(*)操作符与zip函数配合可以实现与zip相反的功能,即将合并的序列拆成多个tuple。
>>>>x=[1,2,3],y=[a,b,c]
>>>>zip(*zip(x,y))
[(1,2,3),(a,b,c)]

高级应用:

1.zip打包解包列表和倍数
>>> a = [1, 2, 3]
>>> b = [a, b, c]
>>> z = zip(a, b)
>>> z
[(1, a), (2, b), (3, c)]
>>> zip(*z)
[(1, 2, 3), (a, b, c)]

2. 使用zip合并相邻的列表项

>>> a = [1, 2, 3, 4, 5, 6]
>>> zip(*([iter(a)] * 2))
[(1, 2), (3, 4), (5, 6)]

>>> group_adjacent = lambda a, k: zip(*([iter(a)] * k))
>>> group_adjacent(a, 3)
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent(a, 2)
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent(a, 1)
[(1,), (2,), (3,), (4,), (5,), (6,)]

>>> zip(a[::2], a[1::2])
[(1, 2), (3, 4), (5, 6)]

>>> zip(a[::3], a[1::3], a[2::3])
[(1, 2, 3), (4, 5, 6)]

>>> group_adjacent = lambda a, k: zip(*(a[i::k] for i in range(k)))
>>> group_adjacent(a, 3)
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent(a, 2)
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent(a, 1)
[(1,), (2,), (3,), (4,), (5,), (6,)]

3.使用zip和iterators生成滑动窗口 (n -grams) 
>>> from itertools import islice
>>> def n_grams(a, n):
...     z = (islice(a, i, None) for i in range(n))
...     return zip(*z)
...
>>> a = [1, 2, 3, 4, 5, 6]
>>> n_grams(a, 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
>>> n_grams(a, 2)
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
>>> n_grams(a, 4)
[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]

4.使用zip反转字典
>>> m = {a: 1, b: 2, c: 3, d: 4}
>>> m.items()
[(a, 1), (c, 3), (b, 2), (d, 4)]
>>> zip(m.values(), m.keys())
[(1, a), (3, c), (2, b), (4, d)]
>>> mi = dict(zip(m.values(), m.keys()))
>>> mi
{1: a, 2: b, 3: c, 4: d}

 

以上是关于python(29)强大的zip函数的主要内容,如果未能解决你的问题,请参考以下文章

13 个非常有用的 Python 代码片段

Python零碎知识:强大的zip

python使用上下文对代码片段进行计时,非装饰器

python之eval函数,map函数,zip函数

记录优雅的pythonic代码

php代码片段: sendFile/videoStream/sendEmail/phpexcel/ffmpeg/zip