Python列表list常用內(nèi)建函數(shù)實(shí)例小結(jié)
本文實(shí)例總結(jié)了Python列表list常用內(nèi)建函數(shù)。分享給大家供大家參考,具體如下:
>>> x = list(range(10)) >>> import random >>> random.shuffle(x) #打亂順序 >>> x [2, 4, 5, 9, 3, 7, 8, 0, 6, 1] >>> max(x) #返回最大值 9 >>> min(x) #返回最小值 0 >>> sum(x) #所有元素之和 45 >>> len(x) #所有元素個(gè)數(shù) 10 >>> list(zip(x,[1]*10)) #多列表重新組合 [(2, 1), (4, 1), (5, 1), (9, 1), (3, 1), (7, 1), (8, 1), (0, 1), (6, 1), (1, 1)] >>> list(zip(range(1,4))) #zip()函數(shù)可以用于一個(gè)序列或者迭代對(duì)象 [(1,), (2,), (3,)] >>> list(zip(['a','b','c'],[1,2])) #兩個(gè)列表不等長(zhǎng),以短的為準(zhǔn) [('a', 1), ('b', 2)] >>> enumerate(x) #枚舉列表元素,返回enumerate對(duì)象 <enumerate object at 0x0000016166A057E0> >>> list(enumerate(x)) #enumerate對(duì)象可迭代 [(0, 2), (1, 4), (2, 5), (3, 9), (4, 3), (5, 7), (6, 8), (7, 0), (8, 6), (9, 1)] >>> x [2, 4, 5, 9, 3, 7, 8, 0, 6, 1]
>>> list(map(str,range(5))) #轉(zhuǎn)換為字符串 ['0', '1', '2', '3', '4'] >>> def add5(v): return v+5
>>> list(map(add5,range(10))) #將單參數(shù)函數(shù)映射到所有元素 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] >>> def add(x,y): return x+y
>>> list(map(add,range(5),range(5,10))) #將雙參數(shù)函數(shù)映射到兩個(gè)序列上 [5, 7, 9, 11, 13] >>> list(map(lambda x,y:x+y, range(5), range(5,10))) [5, 7, 9, 11, 13] >>> [add(x,y) for x, y in zip(range(5), range(5,10))] [5, 7, 9, 11, 13] >>>
標(biāo)準(zhǔn)庫(kù)functools中的reduce()可以將一個(gè)接受2個(gè)參數(shù)的函數(shù)以累積的方式從左到右一次作用到一個(gè)序列或迭代器對(duì)象的所有元素上
>>> from functools import reduce >>> seq = [1,2,3,4,5,6,7,8,9] >>> reduce(lambda x, y:x+y,seq) 45 >>>
>>> seq = ['foo','x33','?!','***'] >>> def func(x): return x.isalnum() #測(cè)試是否為字母或者數(shù)字 >>> filter(func, seq) #返回filter對(duì)象 <filter object at 0x000001B376088BA8> >>> list(filter(func,seq)) #將filter對(duì)象轉(zhuǎn)換為list ['foo', 'x33'] >>> seq ['foo', 'x33', '?!', '***'] >>> [x for x in seq if x.isalnum()] #用列表推導(dǎo)式實(shí)現(xiàn)相同功能 ['foo', 'x33'] >>> list(filter(lambda x:x.isalnum(),seq)) #用lambda實(shí)現(xiàn)相同功能 ['foo', 'x33'] >>> list(filter(None,[1,2,3,0,0,4,0,5])) #指定函數(shù)為None [1, 2, 3, 4, 5] >>>
>>> import random >>> x = [random.randint(1,100) for i in range(10)] #生成10個(gè)1-100區(qū)間的隨機(jī)數(shù) >>> x [72, 11, 80, 97, 94, 75, 70, 21, 21, 41] >>> list(map(lambda i:i+5, x)) #所有元素加5 [77, 16, 85, 102, 99, 80, 75, 26, 26, 46] >>> x = [random.randint(1,10) for i in range(10)] >>> x [5, 7, 6, 2, 6, 1, 5, 1, 2, 7] >>> y = [random.randint(1,10) for i in range(10)] >>> y [2, 10, 9, 7, 7, 4, 9, 1, 7, 1] >>> import operator >>> sum(map(operator.mul, x, y)) #向量?jī)?nèi)積 261 >>> sum((i*j for i,j in zip(x,y))) #使用內(nèi)置函數(shù)計(jì)算向量?jī)?nèi)積 261 >>> list(map(operator.add, x, y)) #兩個(gè)等長(zhǎng)的向量對(duì)應(yīng)元素相加 [7, 17, 15, 9, 13, 5, 14, 2, 9, 8] >>> list(map(lambda i,j: i+j, x,y)) #使用lambda實(shí)現(xiàn)同樣功能 [7, 17, 15, 9, 13, 5, 14, 2, 9, 8] >>>
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專(zhuān)題:《Python列表(list)操作技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python發(fā)送郵件功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了python發(fā)送郵件功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07總結(jié)Python變量的相關(guān)知識(shí)
今天給大家?guī)?lái)的是關(guān)于Python基礎(chǔ)的相關(guān)知識(shí),文章圍繞著Python變量的相關(guān)知識(shí)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例
今天小編就為大家分享一篇Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08PyQt5每天必學(xué)之創(chuàng)建窗口居中效果
這篇文章主要介紹了PyQt5每天必學(xué)之創(chuàng)建窗口居中效果,使應(yīng)用程序窗口顯示在屏幕的中心,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Python當(dāng)中的array數(shù)組對(duì)象實(shí)例詳解
這篇文章主要介紹了Python當(dāng)中的array數(shù)組對(duì)象,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06運(yùn)行獨(dú)立 pyspark 時(shí)出現(xiàn) Windows 錯(cuò)誤解決辦法
在本篇文章里小編給大家分享的是一篇關(guān)于運(yùn)行獨(dú)立 pyspark 時(shí)出現(xiàn) Windows 錯(cuò)誤解決辦法,對(duì)此有需求的方法可以參考下。2021-12-12