最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解python itertools功能

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

介紹

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

      使用只需簡(jiǎn)單一句導(dǎo)入:import itertools

chain()

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

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開始,步長(zhǎng)為2,循環(huán)10,打印對(duì)應(yīng)值;必須手動(dòng)break,count()會(huì)一直循環(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的項(xiàng);
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集中對(duì)應(yīng)的元素。

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

starmap()

      針對(duì)list中的每一項(xiàng),調(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]) 重復(fù)times次;
repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq );當(dāng)函數(shù)f執(zhí)行返回假時(shí), 開始迭代序列
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable);返回序列,當(dāng)predicate為true是截止。
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]);返回序列seq的從start開始到stop結(jié)束的步長(zhǎng)為step的元素的迭代器
for i in islice("abcdef", 0, 4, 2):#a, c
  print i

product()

product(iter1,iter2, ... iterN, [repeat=1]);創(chuàng)建一個(gè)迭代器,生成表示item1,item2等中的項(xiàng)目的笛卡爾積的元組,repeat是一個(gè)關(guān)鍵字參數(shù),指定重復(fù)生成序列的次數(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個(gè)元素做排列的元組的迭代器

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)建一個(gè)迭代器,返回iterable中所有長(zhǎng)度為r的子序列,返回的子序列中的項(xiàng)按輸入iterable中的順序排序

note:不帶重復(fù)

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

同上, 帶重復(fù) 例子:

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三個(gè)數(shù)之和為35的三個(gè)數(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功能,希望對(duì)大家有所幫助!

相關(guān)文章

  • Python數(shù)據(jù)可視化實(shí)踐之使用Matplotlib繪制圖表

    Python數(shù)據(jù)可視化實(shí)踐之使用Matplotlib繪制圖表

    數(shù)據(jù)可視化是數(shù)據(jù)分析的重要環(huán)節(jié),通過將數(shù)據(jù)轉(zhuǎn)化為圖形,可以更直觀地展示數(shù)據(jù)特征和規(guī)律。Python中的Matplotlib庫是一個(gè)強(qiáng)大的數(shù)據(jù)可視化工具,本文將帶您了解Matplotlib的基本使用方法,以及如何繪制常見的圖表
    2023-05-05
  • Python中的引用與copy介紹

    Python中的引用與copy介紹

    這篇文章主要以整型數(shù)據(jù)類型及列表為例,詳細(xì)的介紹了再Python中的引用與copy的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • python fabric使用筆記

    python fabric使用筆記

    這篇文章主要介紹了python fabric使用筆記,fabric是一款實(shí)現(xiàn)遠(yuǎn)程操作和部署強(qiáng)大工具,本文就給出了它的多個(gè)使用實(shí)例,需要的朋友可以參考下
    2015-05-05
  • Python全棧之遞歸函數(shù)

    Python全棧之遞歸函數(shù)

    這篇文章主要為大家介紹了Python遞歸函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • python操作xml文件示例

    python操作xml文件示例

    這篇文章主要介紹了python操作xml文件示例,需要的朋友可以參考下
    2014-04-04
  • python實(shí)現(xiàn)聊天小程序

    python實(shí)現(xiàn)聊天小程序

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)聊天小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python文本的藝術(shù)字符串處理技巧掌握

    Python文本的藝術(shù)字符串處理技巧掌握

    這篇文章主要為大家介紹了Python文本的藝術(shù)字符串處理技巧掌握,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • python 實(shí)現(xiàn)刪除文件或文件夾實(shí)例詳解

    python 實(shí)現(xiàn)刪除文件或文件夾實(shí)例詳解

    這篇文章主要介紹了python 實(shí)現(xiàn)刪除文件或文件夾實(shí)例詳解的相關(guān)資料,這里附有實(shí)例代碼,需要的朋友可以參考下
    2016-12-12
  • 詳解Python魔法方法之描述符類

    詳解Python魔法方法之描述符類

    今天帶大家復(fù)習(xí)一下python描述符類的相關(guān)知識(shí),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Pytorch提取模型特征向量保存至csv的例子

    Pytorch提取模型特征向量保存至csv的例子

    今天小編就為大家分享一篇Pytorch提取模型特征向量保存至csv的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評(píng)論

建昌县| 中西区| 紫云| 苍溪县| 赤壁市| 长沙市| 丽水市| 中牟县| 兖州市| 平塘县| 阜康市| 宜兰县| 略阳县| 南岸区| 六盘水市| 卢湾区| 逊克县| 呼和浩特市| 虞城县| 基隆市| 阿荣旗| 九台市| 双牌县| 克东县| 邛崃市| 淄博市| 通道| 泰顺县| 揭阳市| 温州市| 都江堰市| 桐庐县| 阿拉善右旗| 西充县| 榆社县| 韶关市| 六盘水市| 武宁县| 西乌| 清丰县| 镇原县|