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

詳解python itertools功能

 更新時間:2020年02月07日 09:47:36   作者:neweastsun  
itertools是python內(nèi)置的模塊,使用簡單且功能強大,這里嘗試匯總整理下,并提供簡單應(yīng)用示例,這篇文章主要介紹了python itertools功能,需要的朋友可以參考下

介紹

      itertools是python內(nèi)置的模塊,使用簡單且功能強大,這里嘗試匯總整理下,并提供簡單應(yīng)用示例;如果還不能滿足你的要求,歡迎加入補充。

      使用只需簡單一句導入:import itertools

chain()

      與其名稱意義一樣,給它一個列表如 lists/tuples/iterables,鏈接在一起;返回iterables對象。

letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
   print(list(itertools.chain(letters,booleans)))
#   ['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1]
 
  print(tuple(itertools.chain(letters,letters[3:])))
#   ('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f')
 
  print(set(itertools.chain(letters,letters[3:])))
#   {'a', 'd', 'b', 'e', 'c', 'f'}
    
  print(list(itertools.chain(letters,letters[3:])))
#   ['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f']
 
  for item in list(itertools.chain(letters,booleans)):
    print(item)

count()

  生成無界限序列,count(start=0, step=1) ,示例從100開始,步長為2,循環(huán)10,打印對應(yīng)值;必須手動break,count()會一直循環(huán)。

  i = 0
  for item in itertools.count(100,2):
    i += 1
    if i > 10 : break
    
    print(item) 
 
filterfalse ()
   Python filterfalse(contintion,data) 迭代過濾條件為false的數(shù)據(jù)。如果條件為空,返回data中為false的項;
booleans = [1, 0, 1, 0, 0, 1]
numbers = [23, 20, 44, 32, 7, 12]
 
print(list(itertools.filterfalse(None,booleans)))
#   [0, 0, 0]
print(list(itertools.filterfalse(lambda x : x < 20,numbers)))
#  [23, 20, 44, 32]

compress()

返回我們需要使用的元素,根據(jù)b集合中元素真值,返回a集中對應(yīng)的元素。

print(list(itertools.compress(letters,booleans)))
# ['a', 'c', 'f']

starmap()

      針對list中的每一項,調(diào)用函數(shù)功能。starmap(func,list[]) ;

starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
 
>>> from itertools import *
>>> x = starmap(max,[[5,14,5],[2,34,6],[3,5,2]])
>>> for i in x:
>>> print (i)
14
34
5
repeat()
repeat(object[, times]) 重復times次;
repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq );當函數(shù)f執(zhí)行返回假時, 開始迭代序列
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable);返回序列,當predicate為true是截止。
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]);返回序列seq的從start開始到stop結(jié)束的步長為step的元素的迭代器
for i in islice("abcdef", 0, 4, 2):#a, c
  print i

product()

product(iter1,iter2, ... iterN, [repeat=1]);創(chuàng)建一個迭代器,生成表示item1,item2等中的項目的笛卡爾積的元組,repeat是一個關(guān)鍵字參數(shù),指定重復生成序列的次數(shù)

   

# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
  # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
for i in product([1, 2, 3], [4, 5], [6, 7]):
  print i
(1, 4, 6)
(1, 4, 7)
(1, 5, 6)
(1, 5, 7)
(2, 4, 6)
(2, 4, 7)
(2, 5, 6)
(2, 5, 7)
(3, 4, 6)
(3, 4, 7)
(3, 5, 6)
(3, 5, 7)

permutations()

permutations(p[,r]);返回p中任意取r個元素做排列的元組的迭代器

for i in permutations([1, 2, 3], 3):
  print i
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)


combinations()

combinations(iterable,r);創(chuàng)建一個迭代器,返回iterable中所有長度為r的子序列,返回的子序列中的項按輸入iterable中的順序排序

note:不帶重復

for i in combinations([1, 2, 3], 2):
  print i
(1, 2)
(1, 3)
(2, 3)
combinations_with_replacement()

同上, 帶重復 例子:

for i in combinations_with_replacement([1, 2, 3], 2):
  print i
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)

應(yīng)用示例

求質(zhì)數(shù)序列中1,3,5,7,9,11,13,15三個數(shù)之和為35的三個數(shù);

def get_three_data(data_list,amount):
  for data in list(itertools.combinations(data_list, 3)):
    if sum(data) == amount:
      print(data)
#(7, 13, 15)
#(9, 11, 15)

總結(jié)

以上所述是小編給大家介紹的python itertools功能,希望對大家有所幫助!

相關(guān)文章

  • 舉例介紹Python中的25個隱藏特性

    舉例介紹Python中的25個隱藏特性

    這篇文章主要介紹了一些Python中的隱藏特性,從stackoverflow的人氣問題回答中整理而來,主要以代碼實際解釋說明,需要的朋友可以參考下
    2015-03-03
  • PyTorch CNN實戰(zhàn)之MNIST手寫數(shù)字識別示例

    PyTorch CNN實戰(zhàn)之MNIST手寫數(shù)字識別示例

    本篇文章主要介紹了PyTorch CNN實戰(zhàn)之MNIST手寫數(shù)字識別示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • python requests post多層字典的方法

    python requests post多層字典的方法

    今天小編就為大家分享一篇python requests post多層字典的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 深入理解python虛擬機之多繼承與?mro

    深入理解python虛擬機之多繼承與?mro

    在本篇文章當中將主要給大家介紹?python?當中的多繼承和mro,通過介紹在多繼承當中存在的問題就能夠理解在cpython當中引入c3算法的原因了,從而能夠幫助大家更好的了理解mro,需要的朋友可以參考下
    2023-05-05
  • Python爬蟲爬取微博熱搜保存為 Markdown 文件的源碼

    Python爬蟲爬取微博熱搜保存為 Markdown 文件的源碼

    這篇文章主要介紹了Python爬蟲爬取微博熱搜保存為 Markdown 文件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 使用django和vue進行數(shù)據(jù)交互的方法步驟

    使用django和vue進行數(shù)據(jù)交互的方法步驟

    這篇文章主要介紹了使用django和vue進行數(shù)據(jù)交互的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Pandas中根據(jù)條件替換列中的值的四種方式

    Pandas中根據(jù)條件替換列中的值的四種方式

    本文主要介紹了Pandas中根據(jù)條件替換列中的值的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • Python實現(xiàn)npy/mat文件的保存與讀取

    Python實現(xiàn)npy/mat文件的保存與讀取

    除了常用的csv文件和excel文件之外,我們還可以通過Python把數(shù)據(jù)保存文npy文件格式和mat文件格式。本文為大家展示了實現(xiàn)npy文件與mat文件的保存與讀取的示例代碼,需要的可以參考一下
    2022-04-04
  • 使用python快速獲取PDF文件頁數(shù)的辦法

    使用python快速獲取PDF文件頁數(shù)的辦法

    有時在處理或打印一個PDF文檔之前,你可能需要先知道該文檔包含多少頁,對于程序員來說,編寫腳本來完成這項工作會更加高效,本文就介紹一個使用Python快速獲取PDF文件頁數(shù)的辦法,需要的朋友可以參考下
    2024-03-03
  • python中os和sys模塊的區(qū)別與常用方法總結(jié)

    python中os和sys模塊的區(qū)別與常用方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于python中os和sys模塊的區(qū)別與常用方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。
    2017-11-11

最新評論