python3中zip()函數(shù)使用詳解
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
- python中zip函數(shù)用法詳解(全)
- python內(nèi)置函數(shù)zip詳解
- python 內(nèi)置函數(shù)-range()+zip()+sorted()+map()+reduce()+filter()
- python中zip()函數(shù)遍歷多個(gè)列表方法
- Python中zip函數(shù)如何使用
- python中的 zip函數(shù)詳解及用法舉例
- Python中zip()函數(shù)的簡(jiǎn)單用法舉例
- Python zip()函數(shù)用法實(shí)例分析
- 淺談Python中的zip()與*zip()函數(shù)詳解
- Python中zip()函數(shù)用法實(shí)例教程
- zip在python中的用法小結(jié)
相關(guān)文章
基于CentOS搭建Python Django環(huán)境過(guò)程解析
這篇文章主要介紹了基于CentOS搭建Python Django環(huán)境過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Python棧的實(shí)現(xiàn)方法示例【列表、單鏈表】
這篇文章主要介紹了Python棧的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python基于列表、單鏈表定義棧的相關(guān)操作技巧,需要的朋友可以參考下2020-02-02python+jinja2實(shí)現(xiàn)接口數(shù)據(jù)批量生成工具
這篇文章主要介紹了python+jinja2實(shí)現(xiàn)接口數(shù)據(jù)批量生成工具的操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08python過(guò)濾字符串中不屬于指定集合中字符的類實(shí)例
這篇文章主要介紹了python過(guò)濾字符串中不屬于指定集合中字符的類,涉及Python針對(duì)字符串與集合的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06Python3+Django get/post請(qǐng)求實(shí)現(xiàn)教程詳解
這篇文章主要介紹了Python3+Django get/post請(qǐng)求實(shí)現(xiàn)教程詳解,需要的朋友可以參考下2021-02-02