Python编程:itertools库排列组合
生活随笔
收集整理的這篇文章主要介紹了
Python编程:itertools库排列组合
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
itertools庫包含:
- 無限迭代器
- 有限序列處理
- 排列組合
range對象
# 添加函數(shù)說明 def print_info(obj: "iter object") -> "print_info":print(obj)print(type(obj))print(list(obj))# help(print_info) # print_info(obj:'iter object') -> 'print_info'# range對象 r = range(10) print_info(r) """ range(0, 10) <class 'range'> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] """無限迭代器
import itertools# 無限計數(shù)器,可以指定起始位置和步長 x = itertools.count(start=20, step=-1) print(list(itertools.islice(x, 0, 10, 1))) # [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]# 無限循環(huán)指定的列表和迭代器 x = itertools.cycle("ABC") print(list(itertools.islice(x, 0, 10, 1))) # ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']# 簡單的生成一個擁有指定數(shù)目元素的迭代器 x = itertools.repeat(0, 5) print(list(x)) # [0, 0, 0, 0, 0]有限序列處理
# 累加 x = itertools.accumulate(range(10)) print(x)# <itertools.accumulate object> print(list(x)) # [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]# 連接多個列表或者迭代器 x = itertools.chain(range(3), range(4), [6, 7, 8]) print(x) # <itertools.chain object> print(list(x))# [0, 1, 2, 0, 1, 2, 3, 6, 7, 8]# 按照真值表篩選元素 x = itertools.compress(range(5), (True, False, False, True, True)) print(list(x)) # [0, 3, 4]# 保留對應(yīng)真值為False的元素 x = itertools.filterfalse(lambda e : e < 5, (1, 5, 3, 6, 9, 4)) print(list(x)) # [5, 6, 9]# 按照分組函數(shù)的值對元素進(jìn)行分組 x = itertools.groupby(range(10), lambda e: e < 5 or e > 8) for condition, numbers in x:print(condition, list(numbers)) """ True [0, 1, 2, 3, 4] False [5, 6, 7, 8] True [9] """# 對迭代器進(jìn)行切片 x = itertools.islice(range(10), 0, 9, 2) print(list(x)) # [0, 2, 4, 6, 8]# 按照真值函數(shù)丟棄掉列表和迭代器前面的元素 x = itertools.dropwhile(lambda e: e < 5, range(10)) print(list(x)) # [5, 6, 7, 8, 9]# 與dropwhile相反,保留元素直至真值函數(shù)值為假。 x = itertools.takewhile(lambda e: e < 5, range(10)) print(list(x)) # [0, 1, 2, 3, 4]# 生成指定數(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] """# 類似于zip,不過已較長的列表和迭代器的長度為準(zhǔn) x = itertools.zip_longest(range(3), range(5)) y = zip(range(3), range(5))print(list(x)) print(list(y)) """ [(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)] [(0, 0), (1, 1), (2, 2)] """# 類似map x = itertools.starmap(str.islower, "asdfgDFASDF") y = map(str.islower, "asdfgDFASDF")print(list(x)) print(list(y)) """ [True, True, True, True, True, False, False, False, False, False, False] [True, True, True, True, True, False, False, False, False, False, False] """排列組合
# 產(chǎn)生多個列表和迭代器的(笛卡爾乘積) 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)] """# 求列表或生成器中指定數(shù)目的元素不重復(fù)的所有組合 x = itertools.combinations(range(4), 3) print(type(x)) # <class 'itertools.combinations'> print(list(x)) # [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]# 允許重復(fù)元素的組合 x = itertools.combinations_with_replacement("ABC", 2) print(list(x)) # [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]# 產(chǎn)生指定數(shù)目的元素的所有排列(順序有關(guān)) x = itertools.permutations(range(4), 3) print(list(x)) """ [(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)] """help(itertools)
"""Infinite iterators: 無限迭代器count(start=0, step=1) --> start, start+step, start+2*step, ...cycle(p) --> p0, p1, ... plast, p0, p1, ...repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n timesIterators terminating on the shortest input sequence:accumulate(p[, func]) --> p0, p0+p1, p0+p1+p2chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... chain.from_iterable([p, q, ...]) --> p0, p1, ... plast, q0, q1, ... compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred failsgroupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)filterfalse(pred, seq) --> elements of seq where pred(elem) is Falseislice(seq, [start,] stop [, step]) --> elements fromseq[start:stop:step]starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into ntakewhile(pred, seq) --> seq[0], seq[1], until pred failszip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... Combinatoric generators:product(p, q, ... [repeat=1]) --> cartesian productpermutations(p[, r]) 排列combinations(p, r) 組合combinations_with_replacement(p, r)"""參考:
《相見恨晚的 itertools 庫》
http://mp.weixin.qq.com/s/Rb5aYWA7NYOi1eckGtakuQ
總結(jié)
以上是生活随笔為你收集整理的Python编程:itertools库排列组合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 魔兽世界研究
- 下一篇: JSObject.getWindow()