成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一個零差評的 Python 內(nèi)置庫

開發(fā) 后端
最近事情不是很多,想寫一些技術(shù)文章分享給大家,同時也對自己一段時間來碎片化接受的知識進行一下梳理,所謂寫清楚才能說清楚,說清楚才能想清楚,就是這個道理了。

[[419878]]

 前言

最近事情不是很多,想寫一些技術(shù)文章分享給大家,同時也對自己一段時間來碎片化接受的知識進行一下梳理,所謂寫清楚才能說清楚,說清楚才能想清楚,就是這個道理了。

很多人都致力于把Python代碼寫得更Pythonic,一來更符合規(guī)范且容易閱讀,二來一般Pythonic的代碼在執(zhí)行上也更有效率。今天就先給大家介紹一下Python的系統(tǒng)庫itertools。

itertools庫

迭代器(生成器)在Python中是一種很常用也很好用的數(shù)據(jù)結(jié)構(gòu),比起列表(list)來說,迭代器最大的優(yōu)勢就是延遲計算,按需使用,從而提高開發(fā)體驗和運行效率,以至于在Python 3中map,filter等操作返回的不再是列表而是迭代器。

話雖這么說但大家平時用到的迭代器大概只有range了,而通過iter函數(shù)把列表對象轉(zhuǎn)化為迭代器對象又有點多此一舉,這時候我們今天的主角itertools就該上場了。

使用itertools

itertools中的函數(shù)大多是返回各種迭代器對象,其中很多函數(shù)的作用我們平時要寫很多代碼才能達到,而在運行效率上反而更低,畢竟人家是系統(tǒng)庫。

itertools.accumulate

簡單來說就是累加。 

  1. >>> import itertools  
  2. >>> x = itertools.accumulate(range(10))  
  3. >>> print(list(x))  
  4. [0, 1, 3, 6, 10, 15, 21, 28, 36, 45] 

itertools.chain

連接多個列表或者迭代器。 

  1. >>> x = itertools.chain(range(3), range(4), [3,2,1])  
  2. >>> print(list(x))  
  3. [0, 1, 2, 0, 1, 2, 3, 3, 2, 1] 

itertools.combinations

求列表或生成器中指定數(shù)目的元素不重復(fù)的所有組合 

  1. >>> x = itertools.combinations(range(4), 3)  
  2. >>> print(list(x))  
  3. [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)] 

itertools.combinations_with_replacement

允許重復(fù)元素的組合 

  1. >>> x = itertools.combinations_with_replacement('ABC', 2)  
  2. >>> print(list(x))  
  3. [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')] 

itertools.compress

按照真值表篩選元素 

  1. >>> x = itertools.compress(range(5), (True, False, True, True, False))  
  2. >>> print(list(x))  
  3. [0, 2, 3] 

itertools.count

就是一個計數(shù)器,可以指定起始位置和步長 

  1. >>> x = itertools.count(start=20step=-1)  
  2. >>> print(list(itertools.islice(x, 0, 10, 1)))  
  3. [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] 

itertools.cycle

循環(huán)指定的列表和迭代器 

  1. >>> x = itertools.cycle('ABC')  
  2. >>> print(list(itertools.islice(x, 0, 10, 1)))  
  3. ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']v 

itertools.dropwhile

按照真值函數(shù)丟棄掉列表和迭代器前面的元素 

  1. >>> x = itertools.dropwhile(lambda e: e < 5, range(10))  
  2. >>> print(list(x))  
  3. [5, 6, 7, 8, 9] 

itertools.filterfalse

保留對應(yīng)真值為False的元素 

  1. >>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))  
  2. >>> print(list(x))  
  3. [5, 6, 9] 

itertools.groupby

按照分組函數(shù)的值對元素進行分組 

  1. >>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)                                                                                             
  2. >>> for condition, numbers in x:                                                 
  3. ...     print(condition, list(numbers))                                                                                                         
  4. True [0, 1, 2, 3, 4]                                                               
  5. False [5, 6, 7, 8]                                                                 
  6. True [9] 

itertools.islice

上文使用過的函數(shù),對迭代器進行切片 

  1. >>> x = itertools.islice(range(10), 0, 9, 2)  
  2. >>> print(list(x))  
  3. [0, 2, 4, 6, 8] 

itertools.permutations

產(chǎn)生指定數(shù)目的元素的所有排列(順序有關(guān)) 

  1. >>> x = itertools.permutations(range(4), 3)  
  2. >>> print(list(x))  
  3. [(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0,3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)] 

 itertools.product

產(chǎn)生多個列表和迭代器的(積) 

  1. >>> x = itertools.product('ABC', range(3))  
  2. >>>  
  3. >>> print(list(x))  
  4. [('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)] 

itertools.repeat

簡單的生成一個擁有指定數(shù)目元素的迭代器 

  1. >>> x = itertools.repeat(0, 5)  
  2. >>> print(list(x))  
  3. [0, 0, 0, 0, 0] 

itertools.starmap

類似map 

  1. >>> x = itertools.starmap(str.islower, 'aBCDefGhI')  
  2. >>> print(list(x))  
  3. [True, False, False, False, True, True, False, True, False] 

itertools.takewhile

與dropwhile相反,保留元素直至真值函數(shù)值為假。 

  1. >>> x = itertools.takewhile(lambda e: e < 5, range(10))  
  2. >>> print(list(x))  
  3. [0, 1, 2, 3, 4] 

itertools.tee

這個函數(shù)我也不是很懂,似乎是生成指定數(shù)目的迭代器 

  1. >>> x = itertools.tee(range(10), 2)  
  2. >>> for letters in x:  
  3. ...     print(list(letters))  
  4. ...  
  5. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
  6. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

itertools.zip_longest

類似于zip,不過已較長的列表和迭代器的長度為準 

  1. >>> x = itertools.zip_longest(range(3), range(5))  
  2. >>> y = zip(range(3), range(5))  
  3. >>> print(list(x))  
  4. [(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)]  
  5. >>> print(list(y))  
  6. [(0, 0), (1, 1), (2, 2)] 

結(jié)語

大概就總結(jié)到這里,不過老實說Python的各種語言特性和庫還是要多用才能熟練,最終達到隨手拈來的程度,裝逼的說就是由術(shù)入道。 

 

責(zé)任編輯:龐桂玉 來源: 戀習(xí)Python
相關(guān)推薦

2021-07-29 10:46:56

Python內(nèi)置庫代碼

2022-03-22 06:33:49

Python內(nèi)置模塊函數(shù)

2020-08-29 19:15:09

python數(shù)據(jù)庫SQLite

2016-09-14 17:48:44

2022-06-07 07:21:19

Python內(nèi)置庫命令行

2025-06-04 08:05:00

Peewee?數(shù)據(jù)庫開發(fā)

2025-06-09 10:15:00

FastAPIPython

2025-06-03 10:00:00

LiteLLMPython

2025-05-27 08:00:00

Pythonemoji

2025-06-05 10:00:00

GensimPython

2025-06-03 08:30:00

PotteryRedisPython

2025-06-09 07:25:00

filelock數(shù)據(jù)庫

2025-06-05 08:10:00

PyneconePythonWeb 應(yīng)用

2025-05-29 10:00:00

ZODBPython數(shù)據(jù)庫

2025-06-04 10:05:00

Gooey開源Python

2025-06-10 08:00:00

Pygalpython

2021-08-04 05:49:40

數(shù)據(jù)庫數(shù)時序數(shù)據(jù)庫技術(shù)

2022-09-01 10:46:02

前端組件庫

2025-05-28 08:00:00

Pythonpython-jos開發(fā)

2009-04-20 23:29:12

Oracle收購Sun甲骨文
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 久久久久无码国产精品一区 | 成人精品鲁一区一区二区 | 日韩欧美亚洲一区 | 精品成人一区二区 | 精品欧美一区免费观看α√ | 欧美激情在线精品一区二区三区 | 精品一区国产 | 成人三级av | 国产日韩欧美电影 | 国产精品一区二区三区在线 | 国产精品亚洲精品日韩已方 | 国产免费一区 | 久久专区| 亚洲成人中文字幕 | 91在线看 | 日韩不卡三区 | 午夜激情小视频 | 久久久久国产精品一区二区 | 成人午夜激情 | 2022精品国偷自产免费观看 | 日韩精品久久久久 | 香蕉久久久 | 欧美网址在线观看 | 成人福利在线 | 日韩精品久久久久 | 中文字幕国产视频 | 精品亚洲一区二区 | 国产乱码精品一区二区三区五月婷 | www国产精品 | 国产精品乱码一区二三区小蝌蚪 | 久久久精品网站 | 视频一区二区在线观看 | 欧美综合在线视频 | 激情五月婷婷综合 | 蜜桃视频一区二区三区 | 亚洲美女视频 | 亚洲一区国产 | 涩涩视频在线观看 | 91免费视频 | 日韩高清一区 | 91精品国产综合久久久动漫日韩 |