python zip函数的用法_Python中zip()函数用法

定义:zip([iterable, …])

zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压),看下面的例子就明白了:

>>> a = [1,2,3,4]>>> b = [5,6,7,8]>>> c = [5,6,7,8,9,10]>>> test_zip =zip(a,b)>>>test_zip

[(1, 5), (2, 6), (3, 7), (4, 8)]>>> test_zip1 =zip(a,c)>>>test_zip1

[(1, 5), (2, 6), (3, 7), (4, 8)]>>> test_zip2 =zip(b,c)>>>test_zip2

[(5, 5), (6, 6), (7, 7), (8, 8)]>>> zip(*test_zip)

[(1, 2, 3, 4), (5, 6, 7, 8)]>>>

>>>zip(a,b,c)

[(1, 5, 5), (2, 6, 6), (3, 7, 7), (4, 8, 8)]>>>

例子2:

>>>name

('jack', 'beginman', 'sony', 'pcky')>>>age

(2001, 2003, 2005, 2000)>>> for n,a inzip(name, age):

...printn ,a

...

jack2001beginman2003sony2005pcky2000

>>>

再看一例:

>>> all={"jack":2001,"beginman":2003,"sony":2005,"pcky":2000}>>> for i inall.keys():

...printi, all[i]

...

sony2005pcky2000jack2001beginman2003

>>>

zip()函数:

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

>>> z1 = [1,2,3]>>> z2 = [4,5,6]>>> result =zip(z1,z2)>>>result

[(1, 4), (2, 5), (3, 6)]>>> z3 = [4,5,6,7]>>> result =zip(z1,z3)>>>result

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

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

>>>result

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

>>> zip(*result)

[(1, 2, 3), (4, 5, 6)]>>>result

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

更近一层的了解:

内容来源:http://www.cnblogs.com/diyunpeng/archive/2011/09/15/2177028.html

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

比如我们有一个由列表描述的二维矩阵

a= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

通过python列表推导的方法,我们也能轻易完成这个任务print [ [row[col] for row in a] for col inrange(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 inrange(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 importislice>>> defn_grams(a, n):

... z= (islice(a, i, None) for i inrange(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,zip函数的用法)