<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          一個(gè)零差評(píng)的 Python 內(nèi)置庫(kù)

          共 8180字,需瀏覽 17分鐘

           ·

          2021-08-28 23:01


          前言

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

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

          itertools庫(kù)

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

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

          使用itertools

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

          itertools.accumulate

          簡(jiǎn)單來說就是累加。

          >>> import itertools
          >>> x = itertools.accumulate(range(10))
          >>> print(list(x))
          [0136101521283645]

          itertools.chain

          連接多個(gè)列表或者迭代器。

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

          itertools.combinations

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

          >>> x = itertools.combinations(range(4), 3)
          >>> print(list(x))
          [(012), (013), (023), (123)]

          itertools.combinations_with_replacement

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

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

          itertools.compress

          按照真值表篩選元素

          >>> x = itertools.compress(range(5), (TrueFalseTrueTrueFalse))
          >>> print(list(x))
          [023]

          itertools.count

          就是一個(gè)計(jì)數(shù)器,可以指定起始位置和步長(zhǎng)

          >>> x = itertools.count(start=20, step=-1)
          >>> print(list(itertools.islice(x, 0101)))
          [20191817161514131211]

          itertools.cycle

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

          >>> x = itertools.cycle('ABC')
          >>> print(list(itertools.islice(x, 0101)))
          ['A''B''C''A''B''C''A''B''C''A']

          itertools.dropwhile

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

          >>> x = itertools.dropwhile(lambda e: e < 5, range(10))
          >>> print(list(x))
          [56789]

          itertools.filterfalse

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

          >>> x = itertools.filterfalse(lambda e: e < 5, (153694))
          >>> print(list(x))
          [569]

          itertools.groupby

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

          >>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)                                                                                                
          >>> for condition, numbers in x:                                                  
          ...     print(condition, list(numbers))                                                                                                        
          True [01234]                                                              
          False [5678]                                                                
          True [9]

          itertools.islice

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

          >>> x = itertools.islice(range(10), 092)
          >>> print(list(x))
          [02468]

          itertools.permutations

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

          >>> x = itertools.permutations(range(4), 3)
          >>> print(list(x))
          [(012), (013), (021), (023), (031), (032), (102), (103), (120), (123), (130), (132), (201), (20,3), (210), (213), (230), (231), (301), (302), (310), (312), (320), (321)]

          itertools.product

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

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

          itertools.repeat

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

          >>> x = itertools.repeat(05)
          >>> print(list(x))
          [00000]

          itertools.starmap

          類似map

          >>> x = itertools.starmap(str.islower, 'aBCDefGhI')
          >>> print(list(x))
          [TrueFalseFalseFalseTrueTrueFalseTrueFalse]

          itertools.takewhile

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

          >>> x = itertools.takewhile(lambda e: e < 5, range(10))
          >>> print(list(x))
          [01234]

          itertools.tee

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

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

          itertools.zip_longest

          類似于zip,不過已較長(zhǎng)的列表和迭代器的長(zhǎng)度為準(zhǔn)

          >>> x = itertools.zip_longest(range(3), range(5))
          >>> y = zip(range(3), range(5))
          >>> print(list(x))
          [(00), (11), (22), (None, 3), (None, 4)]
          >>> print(list(y))
          [(00), (11), (22)]

          結(jié)語

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

          來源:憶先    
          鏈接:https://segmentfault.com/a/1190000008590958

          瀏覽 30
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品久久久久久无码牛牛章艳 | 日本亚洲在线观看 | 国产美女日逼视频 | 色欲熟妇 | 日本熟妇HD |