欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3中zip()函數(shù)使用詳解

 更新時(shí)間:2018年06月29日 08:51:08   投稿:hebedich  
zip函數(shù)接受任意多個(gè)可迭代對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)tuple,然后返回一個(gè)可迭代的zip對(duì)象.這個(gè)可迭代對(duì)象可以使用循環(huán)的方式列出其元素,若多個(gè)可迭代對(duì)象的長(zhǎng)度不一致,則所返回的列表與長(zhǎng)度最短的可迭代對(duì)象相同.

zip在python3中,處于優(yōu)化內(nèi)存的考慮,只能訪問(wèn)一次!??!(python2中可以訪問(wèn)多次),童鞋們一定要注意,

* coding: utf-8 * 


zip()函數(shù)的定義:從參數(shù)中的多個(gè)迭代器取元素組合成一個(gè)新的迭代器;
返回: 返回一個(gè)zip對(duì)象,其內(nèi)部元素為元組;可以轉(zhuǎn)化為列表或元組;
傳入?yún)?shù): 元組、列表、字典等迭代器。
當(dāng)zip()函數(shù)中只有一個(gè)參數(shù)時(shí),zip(iterable)從iterable中依次取一個(gè)元組,組成一個(gè)元組。

在python 3.0中有個(gè)大坑,zip中的數(shù)據(jù)只能操作一次,內(nèi)存就會(huì)釋放,當(dāng)下次

訪問(wèn)時(shí)就會(huì)報(bào)錯(cuò),例如例子1中的輸出操作

1、zip()函數(shù)單個(gè)參數(shù)

print(‘=‘*10 + “zip()函數(shù)單個(gè)參數(shù)” + ‘=‘*10) 
list1 = [1, 2, 3, 4] 
tuple1 = zip(list1)

list2=list(tuple1)

打印zip函數(shù)的返回類型

print(“zip()函數(shù)的返回類型:\n”, type(tuple1))#類型為

將zip對(duì)象轉(zhuǎn)化為列表

print(“zip對(duì)象轉(zhuǎn)化為列表:\n”, list(tuple1))#值為[(1,), (2,), (3,), (4,)] 
print(“zip對(duì)象轉(zhuǎn)化為列表:\n”, list(tuple1))#值為[]

print(“l(fā)ist2輸出的列表1為:\n”,list2) 
print(“l(fā)ist2輸出的列表2為:\n”,list2) 

當(dāng)zip()函數(shù)有兩個(gè)參數(shù)時(shí):zip(a,b)zip()函數(shù)分別從a和b依次各取出一個(gè)元素組成
元組,再將依次組成的元組組合成一個(gè)新的迭代器–新的zip類型數(shù)據(jù)。
注意:要求a與b的維數(shù)相同,當(dāng)兩者具有相同的行數(shù)與列數(shù)時(shí),正常組合對(duì)應(yīng)位置元素即可;
當(dāng)a與b的行數(shù)或列數(shù)不同時(shí),取兩者結(jié)構(gòu)中最小的行數(shù)和列數(shù),依照最小的行數(shù)和列數(shù)將
對(duì)應(yīng)位置的元素進(jìn)行組合;這時(shí)相當(dāng)于調(diào)用itertools.zip_longest(*iterables)函數(shù)。

2、zip()函數(shù)有2個(gè)參數(shù)

print(‘=‘*10 + “zip()函數(shù)有2個(gè)參數(shù)” + ‘=‘*10) 
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
n = [[2, 2, 2], [3, 3, 3], [4, 4, 4]] 
p = [[2, 2, 2], [3, 3, 3]]

行與列相同

print(“行與列相同:\n”, list(zip(m, n)))

值為[([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3]), ([7, 8, 9], [4, 4, 4])]

行與列不同

print(“行與列不同:\n”, list(zip(m, p)))

值為[([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3])]

3、zip()應(yīng)用,也可以使用for循環(huán)+列表推導(dǎo)式實(shí)現(xiàn)

矩陣相加減、點(diǎn)乘

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

矩陣點(diǎn)乘

print(‘=‘*10 + “矩陣點(diǎn)乘” + ‘='10)#左右兩端各有10個(gè) 
print([x*y for a, b in zip(m, n) for x, y in zip(a, b)])

[2, 4, 6, 12, 15, 18, 28, 32, 36]

矩陣相加,相減雷同

print(‘=‘*10 + “矩陣相加,相減” + ‘=‘*10) 
print([x+y for a, b in zip(m, n) for x, y in zip(a, b)])

[3, 4, 5, 7, 8, 9, 11, 12, 13]

4、*zip的操作

m5=[1,2,3] 
n5=[4,5,6] 
k5=[7,8,9] 
zip5=zip(m5,n5,k5)

print(“l(fā)ist(zip5):”,list(zip5))不能輸出,否則zip(*zip5)

就無(wú)法執(zhí)行

m6,n6,k6=zip(*zip5) 
print(“m6:”,m6)#m6: (1, 2, 3) 
print(“n6:”,n6)#n6: (4, 5, 6) 
print(“k6:”,k6)#k6: (7, 8, 9)

5、*zip()函數(shù)

*zip()函數(shù)是zip()函數(shù)的逆過(guò)程,將zip對(duì)象變成原先組合前的數(shù)據(jù)。

print(‘=‘*10 + “*zip()函數(shù)” + ‘=‘*10) 
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
n = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]

print(“zip(m, n)返回:\n”, zip(m, n)) # 
print(“*zip(m, n)返回:\n”, *zip(m, n))

*zip(m, n)返回:([1, 2, 3], [2, 2, 2]) ([4, 5, 6],[3, 3, 3]) ([7, 8, 9], [4, 4, 4])

print(“l(fā)ist(zip(m, n))返回:\n”, list(zip(m, n)))

list(zip(m, n))返回: [([1, 2, 3], [2, 2, 2]), ([4, 5, 6], [3, 3, 3]), ([7, 8, 9], [4, 4, 4])]

m2, n2 = zip(*zip(m, n))#先合到一塊 
print(“m2:”,m2)#([1, 2, 3], [4, 5, 6], [7, 8, 9]) 
print(“n2:”,n2)#([2, 2, 2], [3, 3, 3], [4, 4, 4]) 
print(m == list(m2) and n == list(n2))#true

相關(guān)文章

最新評(píng)論