python(29)強大的zip函式

2022-05-06 07:57:09 字數 4167 閱讀 6194

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 21lihua 14zhang 34

>>>

**二:

>>> all=

>>> for i in

all.keys():

...

print

i,all[i]

...hell 20helo 23lei 19sony 15

>>>

**三:若傳入引數的長度不等,則返回list的長度和引數中長度最短的物件相同。

>>> aa = ("

1","

3","5"

)>>> bb = ("

ee","tt"

)>>> for a,n in

zip(aa,bb):

...

print

a,n...

1ee3tt

>>>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'

),)]

>>>

擴寬:

*二維矩陣變換(矩陣的行列互換)

比如我們有乙個由列表描述的二維矩陣

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 =

>>>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

python(29)強大的zip函式

zip函式 它是python的內建函式,與序列有關的內建函式有 sorted reversed enumerate zip 其中sorted 和zip 返回乙個序列 列表 物件,reversed enumerate 返回乙個迭代器 類似序列 一 name lilei lihua zhang age ...

python 29 執行緒與程序

執行緒呼叫兩種方式 函式和類 import threading import time def funs num 定義每個執行緒要執行的函式 print funs thread on number s num time.sleep 3 class mythread threading.thread ...

python 29 執行緒與程序

執行緒呼叫兩種方式 函式和類 import threading import time def funs num 定義每個執行緒要執行的函式 print funs thread on number s num time.sleep 3 class mythread threading.thread ...