来源于小甲鱼题目 请用两行代码找出"FishC"五个字母的所有组合 便看了看itertools库里面的内容
导入itertools库,其作用是用于创建有效循环的迭代器的函数
import itertools
print(*("第 %s 个的值为:%s" % (i + 1, ''.join(val)) for i, val in enumerate(list(itertools.permutations('fishc', 5)))), sep = '\n')
输出结果为:
第 1 个的值为:fishc
第 2 个的值为:fisch
第 3 个的值为:fihsc
第 4 个的值为:fihcs
第 5 个的值为:ficsh
第 6 个的值为:fichs
……
其中使用到了 ''.join() 和enumerate() 函数
前者是python简写的一个方法,如','.join(map(lambda x:str(x),list))就是循环将list的每个元素,都变成字符类型。
后者用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。如:
list1 = ['A', 'B', 'C']
for i, element in enumerate(list1):
print(i, element)
>>> 0 one
1 two
2 three
具体例子可参考 菜鸟教程 https://www.runoob.com/python/python-func-enumerate.html
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1)) # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
接下来便是itertools的用法啦……
Infinite iterators(无限迭代器):
Iterator | Arguments | Results | Example |
---|---|---|---|
start, [step] | start, start+step, start+2*step, … |
| |
p | p0, p1, … plast, p0, p1, … |
| |
elem [,n] | elem, elem, elem, … endlessly or up to n times |
|
Iterators terminating on the shortest input sequence(迭代器以最短的输入序列终止)::
Iterator | Arguments | Results | Example |
---|---|---|---|
p [,func] | p0, p0+p1, p0+p1+p2, … |
| |
p, q, … | p0, p1, … plast, q0, q1, … |
| |
iterable | p0, p1, … plast, q0, q1, … |
| |
data, selectors | (d[0] if s[0]), (d[1] if s[1]), … |
| |
pred, seq | seq[n], seq[n+1], starting when pred fails |
| |
pred, seq | elements of seq where pred(elem) is false |
| |
iterable[, key] | sub-iterators grouped by value of key(v) | ||
seq, [start,] stop [, step] | elements from seq[start:stop:step] |
| |
func, seq | func(*seq[0]), func(*seq[1]), … |
| |
pred, seq | seq[0], seq[1], until pred fails |
| |
it, n | it1, it2, … itn splits one iterator into n | ||
p, q, … | (p[0], q[0]), (p[1], q[1]), … |
|
Combinatoric iterators(组合迭代器):
Iterator | Arguments | Results |
---|---|---|
p, q, … [repeat=1] | cartesian product, equivalent to a nested for-loop | |
p[, r] | r-length tuples, all possible orderings, no repeated elements | |
p, r | r-length tuples, in sorted order, no repeated elements | |
p, r | r-length tuples, in sorted order, with repeated elements | |
|
| |
|
| |
|
| |
|
|
几个小例子:
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list(accumulate(data, operator.mul)) # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list(accumulate(data, max)) # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt))
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
# Chaotic recurrence relation https://en.wikipedia.org/wiki/Logistic_map
>>> logistic_map = lambda x, _: r * x * (1 - x)
>>> r = 3.8
>>> x0 = 0.4
>>> inputs = repeat(x0, 36) # only the initial value is used
>>> [format(x, '.2f') for x in accumulate(inputs, logistic_map)]
['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63',
'0.88', '0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57',
'0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',
'0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']