Python中的高效迭代库itertools,排列组合随便求
共 23217字,需浏览 47分钟
·
2021-02-09 20:55
10 11 12 13 14 ...","26:\"9944989\"|27:\"10\""],[20,"\n","7:0"]],"25:\"fnyq\"|27:\"10\"|7:0"],"A3":[40,[[20,"cycle()","0:\"%236363bb\"|16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.cycle\"|1:\"transparent\"|26:\"9944989\"|27:\"10\"|31:2"],[20,"\n","24:\"iTOY\"|7:0"]],"25:\"Myw4\"|27:\"10\"|7:0"],"B3":[40,[[20,"p","26:\"9944989\"|27:\"10\""],[20,"\n","24:\"KxjC\"|7:0"]],"25:\"QqJC\"|27:\"10\"|7:0"],"C3":[40,[[20,"p0, p1, ... plast, p0, p1, ...","26:\"9944989\"|27:\"10\""],[20,"\n","24:\"E7lo\"|7:0"]],"25:\"YShY\"|27:\"10\"|7:0"],"D3":[40,[[20,"cycle('ABCD') --> A B C D A B C D ...","26:\"9944989\"|27:\"10\""],[20,"\n","7:0"]],"25:\"4fFo\"|27:\"10\"|7:0"],"A4":[40,[[20,"repeat()","0:\"%236363bb\"|16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.repeat\"|1:\"transparent\"|26:\"9944989\"|27:\"10\"|31:2"],[20,"\n","24:\"hLDE\"|7:0"]],"25:\"Br9Q\"|27:\"10\"|7:0"],"B4":[40,[[20,"elem [,n]","26:\"9944989\"|27:\"10\""],[20,"\n","24:\"FZvr\"|7:0"]],"25:\"jYVu\"|27:\"10\"|7:0"],"C4":[40,[[20,"elem, elem, elem, ... 重复无限次或n次","26:\"9944989\"|27:\"10\""],[20,"\n","24:\"RLTb\"|7:0"]],"25:\"qShA\"|27:\"10\"|7:0"],"D4":[40,[[20,"repeat(10, 3) --> 10 10 10","26:\"9944989\"|27:\"10\""],[20,"\n","7:0"]],"25:\"4eTj\"|27:\"10\"|7:0"]},[[10,1,"26:\"9944989\"|4:32"],[10,3,"26:\"9944989\"|4:54"]],[[10,1,"3:88"],[10,1,"3:75"],[10,1,"3:196"],[10,1,"3:257"]]],"25:\"hqIZvO\"|readOnly:false"],[20,"3.1 ","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"count()","0:\"%23ff0000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"yGhp\"|32:2"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"返回以start开始,step递增的序列,无限递增","27:\"10\"|31:2"],[20,"\n","24:\"HaE1\""],[20,"语法:itertools.count(start=0,step=1)","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"I8XX\"|7:3"],[20,"for each in itertools.count(start=0, step=2):"],[20,"\n","24:\"U26I\"|36:131"],[20," print (each)"],[20,"\n","24:\"lBOO\"|36:131"],[20,"1"],[20,"\n","24:\"Nrss\"|36:131"],[20,"2"],[20,"\n","24:\"Gq3C\"|36:131"],[20,"..."],[20,"\n","24:\"i1ye\"|36:131"],[20,"100003"],[20,"\n","24:\"47Uk\"|36:131"],[20,"\n","24:\"SfiE\"|36:131"],[20,"'''小应用, 当取到第三个 A 时停止'''"],[20,"\n","24:\"WHd1\"|36:131"],[20,"test_str = ''"],[20,"\n","24:\"UakC\"|36:131"],[20,"for i in itertools.cycle('ABC'):"],[20,"\n","24:\"jaMI\"|36:131"],[20," test_str += i"],[20,"\n","24:\"IQG8\"|36:131"],[20," if test_str.count('A') >= 3:"],[20,"\n","24:\"hVYt\"|36:131"],[20," break"],[20,"\n","24:\"lqdD\"|36:131"],[20,"print(test_str)"],[20,"\n","24:\"5nrc\"|36:131"],[20,"# ABCABCA"],[20,"\n","24:\"p9aL\"|36:131"],[20," ","27:\"10\""],[20,"\n","24:\"FpGU\"|32:2|7:3"],[20,"3.2 cycle()","0:\"%23c00000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"2q8o\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"将迭代器进行无限迭代","27:\"10\"|31:2"],[20,"\n","24:\"n5Rp\""],[20,"语法:","27:\"10\"|31:2|8:1"],[20,"itertools.cycle(iterable)","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"7TxA\"|7:3"],[20,"for each in itertools.cycle('ab'):"],[20,"\n","24:\"m5o7\"|36:131"],[20," print(each)"],[20,"\n","24:\"qH5P\"|36:131"],[20,"a"],[20,"\n","24:\"zxH0\"|36:131"],[20,"b"],[20,"\n","24:\"YdXp\"|36:131"],[20,"..."],[20,"\n","24:\"FCiB\"|36:131"],[20,"b"],[20,"\n","24:\"tT52\"|36:131"],[20,"a"],[20,"\n","24:\"hKhx\"|36:131"],[20,"3.3 repeat()","0:\"%23c00000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"Mniw\"|32:2"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"不停的返回object对象,如果指定了times,则返回times次 ","27:\"10\"|31:2"],[20,"\n","24:\"W23a\""],[20,"语法:itertools.repeat(object,[,times])","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"gmH1\""],[20,"for each in itertools.repeat('ab', 2):"],[20,"\n","24:\"7nhV\"|36:131"],[20,"print(each)"],[20,"\n","24:\"NZQC\"|36:131"],[20,"ab"],[20,"\n","24:\"vSWc\"|36:131"],[20,"ab "],[20,"\n","24:\"CKhD\"|36:131"],[20,"\n","24:\"NeK2\"|7:3"],[20,"四、有限迭代器","0:\"%23000000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"Ed8n\"|32:1|7:3"],[30,[{"A1":[40,[[20,"迭代器","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"Tibn\"|27:\"10\""],"B1":[40,[[20,"实参","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"XI5L\"|27:\"10\""],"C1":[40,[[20,"结果","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"xP3o\"|27:\"10\""],"D1":[40,[[20,"示例","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"qapB\"|27:\"10\""],"A2":[40,[[20,"accumulate()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.accumulate\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"T6HN\"|27:\"10\""],"B2":[40,[[20,"p [,func]","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"G7SR\"|27:\"10\""],"C2":[40,[[20,"p0, p0+p1, p0+p1+p2, ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"cOFj\"|27:\"10\""],"D2":[40,[[20,"accumulate([1,2,3,4,5]) --> 1 3 6 10 15","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"WgJx\"|27:\"10\""],"A3":[40,[[20,"chain()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.chain\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"sjGa\"|27:\"10\""],"B3":[40,[[20,"p, q, ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"U2X6\"|27:\"10\""],"C3":[40,[[20,"p0, p1, ... plast, q0, q1, ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"CY0T\"|27:\"10\""],"D3":[40,[[20,"chain('ABC', 'DEF') --> A B C D E F","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"lDp8\"|27:\"10\""],"A4":[40,[[20,"chain.from_iterable()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.chain.from_iterable\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"D3zF\"|27:\"10\""],"B4":[40,[[20,"iterable","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"ARqg\"|27:\"10\""],"C4":[40,[[20,"p0, p1, ... plast, q0, q1, ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"WekN\"|27:\"10\""],"D4":[40,[[20,"chain.from_iterable(['ABC', 'DEF']) --> A B C D E F","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"4qxW\"|27:\"10\""],"A5":[40,[[20,"compress()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.compress\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"6dtq\"|27:\"10\""],"B5":[40,[[20,"data, selectors","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"uKNz\"|27:\"10\""],"C5":[40,[[20,"(d[0] if s[0]), (d[1] if s[1]), ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"kHEO\"|27:\"10\""],"D5":[40,[[20,"compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"wiWn\"|27:\"10\""],"A6":[40,[[20,"dropwhile()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.dropwhile\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"h7MY\"|27:\"10\""],"B6":[40,[[20,"pred, seq","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"3nTF\"|27:\"10\""],"C6":[40,[[20,"seq[n], seq[n+1], ... 从pred首次真值测试失败开始","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"Ykdq\"|27:\"10\""],"D6":[40,[[20,"dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"JpHo\"|27:\"10\""],"A7":[40,[[20,"filterfalse()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.filterfalse\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"zrp3\"|27:\"10\""],"B7":[40,[[20,"pred, seq","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"p6DN\"|27:\"10\""],"C7":[40,[[20,"seq中pred(x)为假值的元素,x是seq中的元素。","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"fqjg\"|27:\"10\""],"D7":[40,[[20,"filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"Lfh0\"|27:\"10\""],"A8":[40,[[20,"groupby()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.groupby\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"B733\"|27:\"10\""],"B8":[40,[[20,"iterable[, key]","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"CKfZ\"|27:\"10\""],"C8":[40,[[20,"根据key(v)值分组的迭代器","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"jYfN\"|27:\"10\""],"D8":[40,[[20,"\n"]],"25:\"cCHy\"|27:\"10\""],"A9":[40,[[20,"islice()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.islice\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"mYuQ\"|27:\"10\""],"B9":[40,[[20,"seq, [start,] stop [, step]","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"k4TD\"|27:\"10\""],"C9":[40,[[20,"seq[start:stop:step]中的元素","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"NYxX\"|27:\"10\""],"D9":[40,[[20,"islice('ABCDEFG', 2, None) --> C D E F G","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"MmFL\"|27:\"10\""],"A10":[40,[[20,"starmap()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.starmap\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"5xqq\"|27:\"10\""],"B10":[40,[[20,"func, seq","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"G2SD\"|27:\"10\""],"C10":[40,[[20,"func(*seq[0]), func(*seq[1]), ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"pwo0\"|27:\"10\""],"D10":[40,[[20,"starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"MldL\"|27:\"10\""],"A11":[40,[[20,"takewhile()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.takewhile\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"bTOK\"|27:\"10\""],"B11":[40,[[20,"pred, seq","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"N37x\"|27:\"10\""],"C11":[40,[[20,"seq[0], seq[1], ..., 直到pred真值测试失败","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"RVgR\"|27:\"10\""],"D11":[40,[[20,"takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"NIkQ\"|27:\"10\""],"A12":[40,[[20,"tee()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.tee\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"5cZJ\"|27:\"10\""],"B12":[40,[[20,"it, n","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"0yVp\"|27:\"10\""],"C12":[40,[[20,"it1, it2, ... itn 将一个迭代器拆分为n个迭代器","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"q1Ze\"|27:\"10\""],"D12":[40,[[20,"\n"]],"25:\"T0hJ\"|27:\"10\""],"A13":[40,[[20,"zip_longest()","16:\"https%3A%2F%2Fdocs.python.org%2Fzh-cn%2F3%2Flibrary%2Fitertools.html%23itertools.zip_longest\"|26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"dgsR\"|27:\"10\""],"B13":[40,[[20,"p, q, ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"Rxdy\"|27:\"10\""],"C13":[40,[[20,"(p[0], q[0]), (p[1], q[1]), ...","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"02AM\"|27:\"10\""],"D13":[40,[[20,"zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-","26:\"9944989\"|27:\"10\""],[20,"\n"]],"25:\"KHS5\"|27:\"10\""]},[[10,1,"26:\"9944989\"|4:32"],[10,4,"26:\"9944989\"|4:54"],[10,2,"26:\"9944989\"|4:76"],[10,1,"26:\"9944989\"|4:54"],[10,1,"26:\"9944989\"|4:98"],[10,1,"26:\"9944989\"|4:54"],[10,2,"26:\"9944989\"|4:76"],[10,1,"26:\"9944989\"|4:54"]],[[10,1,"26:\"9944989\"|3:147"],[10,1,"26:\"9944989\"|3:105"],[10,1,"26:\"9944989\"|3:112"],[10,1,"26:\"9944989\"|3:252"]]],"25:\"keQmJ6\"|readOnly:false"],[20,"4.1 ","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"accumulate()","0:\"%23ff0000\"|27:\"10\""],[20,"\n","24:\"HjNo\"|32:2|7:3"],[20,"作用:","0:\"%23000000\"|27:\"10\"|31:2|8:1"],[20,"创建一个迭代器,返回累积汇总值或其他双目运算函数的累积结果值(通过可选的 func 参数指定),,若传入 func,则按照 func 中的方法执行。","27:\"10\""],[20,"\n","24:\"CkAe\"|7:3"],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"itertools.accumulate(iterable[, func, ","0:\"%2337d9f0\"|27:\"10\"|31:2"],[20," ","27:\"10\""],[20,"*, initial=None])","0:\"%2337d9f0\"|27:\"10\"|31:2"],[20,"\n","24:\"4FFE\"|7:3"],[20,"iterable: 可迭代对象","27:\"10\""],[20,"\n","24:\"qPtA\"|27:\"10\"|7:3|bullet-id:\"0Mut\"|bullet:\"ring\""],[20,"func: 带有两个参数的函数, 可选参数,func 参数有几种用法。它可以被设为 min() 最终得到一个最小值,或者设为 max() 最终得到一个最大值,或设为 operator.mul() 最终得到一个乘积。摊销表可通过累加利息和支付款项得到。给iterable设置初始值并只将参数 func 设为累加总数可以对一阶 ","27:\"10\""],[20,"递归关系","16:\"https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRecurrence_relation\"|27:\"10\""],[20," 建模。 ","27:\"10\""],[20,"\n","24:\"0WJa\"|27:\"10\"|7:3|bullet-id:\"0Mut\"|bullet:\"ring\""],[20,"initial: 关键字参数, 默认为 None, 若此参数传参, 则此参数作为 iterable 的第一个元素","27:\"10\""],[20,"\n","24:\"66EG\"|27:\"10\"|7:3|bullet-id:\"0Mut\"|bullet:\"ring\""],[20,"'''默认情况, 返回累计汇总值'''"],[20,"\n","24:\"nO4Q\"|36:131"],[20,"list(itertools.accumulate([1, 2, 3, 4, 5]))"],[20,"\n","24:\"I8cA\"|36:131"],[20,"[1, 3, 6, 10, 15]"],[20,"\n","24:\"gXoa\"|36:131"],[20,"\n","24:\"lY7L\"|36:131"],[20,"'''方法修改为计算累计的乘积'''"],[20,"\n","24:\"MdwR\"|36:131"],[20,"import operator #该模块输出一系列对应Python内部操作符的函数"],[20,"\n","24:\"YiiA\"|36:131"],[20,"data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]"],[20,"\n","24:\"lym5\"|36:131"],[20,"list(itertools.accumulate(data, operator.mul)) "],[20,"\n","24:\"OwNH\"|36:131"],[20,"[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]"],[20,"\n","24:\"ZNLb\"|36:131"],[20,"\n","24:\"KoSX\"|36:131"],[20,"'''方法修改为计算累计的最大值'''"],[20,"\n","24:\"aydP\"|36:131"],[20,"list(itertools.accumulate(data, max))"],[20,"\n","24:\"I0NH\"|36:131"],[20,"[3, 4, 6, 6, 6, 9, 9, 9, 9, 9] "],[20,"\n","24:\"6G4g\"|36:131"],[20,"\n","24:\"MpaN\"|36:131"],[20,"# Amortize a 5% loan of 1000 with 4 annual payments of 90"],[20,"\n","24:\"2bfx\"|36:131"],[20,"cashflows = [1000, -90, -90, -90, -90]"],[20,"\n","24:\"uPLZ\"|36:131"],[20,"list(itertools.accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt))"],[20,"\n","24:\"nabF\"|36:131"],[20,"[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]"],[20,"\n","24:\"ieUs\"|36:131"],[20,"\n","24:\"yK2C\"|36:131"],[20,"#Chaotic recurrence relation "],[20,"\n","24:\"poxx\"|36:131"],[20,"#https://en.wikipedia.org/wiki/Logistic_map"],[20,"\n","24:\"XR8D\"|36:131"],[20,"logistic_map = lambda x, _: r * x * (1 - x)"],[20,"\n","24:\"UxZx\"|36:131"],[20,"r = 3.8"],[20,"\n","24:\"fJd3\"|36:131"],[20,"x0 = 0.4"],[20,"\n","24:\"G5Yw\"|36:131"],[20,"inputs = repeat(x0, 36) # only the initial value is used"],[20,"\n","24:\"xY08\"|36:131"],[20,"[format(x, '.2f') for x in accumulate(inputs, logistic_map)]"],[20,"\n","24:\"Z9j6\"|36:131"],[20,"['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63',"],[20,"\n","24:\"ow7l\"|36:131"],[20," '0.88', '0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57',"],[20,"\n","24:\"nxBR\"|36:131"],[20," '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',"],[20,"\n","24:\"IFBP\"|36:131"],[20," '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']"],[20,"\n","24:\"D0YK\"|36:131"],[20,"\n","24:\"eUhF\"|36:131"],[20,"'''在迭代器起始位置添加一个元素, 方法不变, 返回累计汇总值'''"],[20,"\n","24:\"xhCa\"|36:131"],[20,"list(itertools.accumulate([1, 2, 3, 4, 5], initial=100))"],[20,"\n","24:\"dcxW\"|36:131"],[20,"# [100, 101, 103, 106, 110, 115]"],[20,"\n","24:\"td37\"|36:131"],[20,"\n","24:\"kZjR\"|36:131"],[20,"'''在迭代器起始位置添加一个元素, 方法修改为计算累计的乘积'''"],[20,"\n","24:\"RSeD\"|36:131"],[20,"list(itertools.accumulate([1, 2, 3, 4, 5], lambda x, y: x * y, initial=10))"],[20,"\n","24:\"flSm\"|36:131"],[20,"#[10, 10, 20, 60, 240, 1200]"],[20,"\n","24:\"jL8j\"|36:131"],[20," ","27:\"10\""],[20,"\n","24:\"1ypc\"|7:3"],[20,"4.2 ","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"chain()","0:\"%23ff0000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"mzuQ\"|32:2|7:3"],[20,"作用:","0:\"%23000000\"|27:\"10\"|31:2|8:1"],[20,"chain 接收多个可迭代对象作为参数,将它们『连接』起来,作为一个新的迭代器返回。","27:\"10\""],[20,"\n","24:\"JCZZ\"|7:3"],[20,"语法:chain(iterable1, iterable2, iterable3, ...)","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"siSl\"|7:3"],[20," ","27:\"10\"|31:2"],[20,"\n","24:\"IFdQ\"|7:3"],[20,"list(itertools.chain([1, 2, 3], ('A', 'B', 'C'), {'一', '二', '三'}))"],[20,"\n","24:\"3qC1\"|36:131"],[20,"[1, 2, 3, 'A', 'B', 'C', '二', '一', '三']"],[20,"\n","24:\"UNs1\"|36:131"],[20,"\n","24:\"O73V\"|36:131"],[20,"list(itertools.chain( 'ABC', 'DEF'))"],[20,"\n","24:\"SBwU\"|36:131"],[20,"['A', 'B', 'C', 'D', 'E', 'F']"],[20,"\n","24:\"fYM6\"|36:131"],[20,"4.3 chain.from_iterable","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"5dxc\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"与 itertools.chain() 函数类似,但是参数是一个可迭代对象,将这个可迭代对象中元素一一添加到新迭代器中,如果元素是一个可迭代对象,那么会将这个元素内的元素一一添加到新迭代器中。小编自己的理解就是迭代器降维。","27:\"10\""],[20,"\n","24:\"rSKv\""],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|8:1"],[20,"itertools.chain.from_iterable(iterable)","0:\"%2337d9f0\"|27:\"10\"|31:2"],[20,"\n","24:\"ghDp\"|7:3"],[20,"'''将二维迭代器降维'''"],[20,"\n","24:\"gItH\"|36:131"],[20,"temp = itertools.chain.from_iterable(['1', ['2', '3'], ('4', '5')])"],[20,"\n","24:\"YCDz\"|36:131"],[20,"print(list(temp))"],[20,"\n","24:\"sl6Y\"|36:131"],[20,"# ['1', '2', '3', '4', '5']"],[20,"\n","24:\"0z3H\"|36:131"],[20,"\n","24:\"Es2L\"|36:131"],[20,"'''只能降一层维度, 三维将至二维'''"],[20,"\n","24:\"eifK\"|36:131"],[20,"temp = itertools.chain.from_iterable(['1', ['2', '3'], ('4', ['5', '6'])])"],[20,"\n","24:\"8VtU\"|36:131"],[20,"print(list(temp))"],[20,"\n","24:\"X9K3\"|36:131"],[20,"# ['1', '2', '3', '4', ['5', '6']]"],[20,"\n","24:\"4lSc\"|36:131"],[20,"PS: 迭代器维度概念可以理解为,整个迭代器中的元素都不是迭代器类型的就是一维,迭代器中的元素有是迭代器的就是二维,迭代器中的元素有是迭代器的,然后这个迭代器中还有元素是迭代器的就是三维,依此类推。","27:\"10\"|31:2"],[20,"\n","24:\"uani\"|7:3"],[20," ","27:\"10\""],[20,"\n","24:\"FvXs\"|7:3"],[20,"4.4 compress","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"EMA7\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"创建一个迭代器,将 data 中经过 selectors 真值测试为 True 的元素保留。当两个可迭代对象中的某一个到达末尾时执行停止,返回最终结果。","27:\"10\""],[20,"\n","24:\"OoSs\""],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|8:1"],[20,"compress(data, selectors)","0:\"%2337d9f0\"|27:\"10\""],[20,"\n","24:\"mbu1\""],[20,"data: 可迭代对象, 包含所需的数据","27:\"10\""],[20,"\n","24:\"KWAn\"|27:\"10\"|bullet-id:\"IgOu\"|bullet:\"ring\""],[20,"selectors: 可迭代对象, 真值测试数据","27:\"10\""],[20,"\n","24:\"KZj3\"|27:\"10\"|bullet-id:\"IgOu\"|bullet:\"ring\""],[20,"for each in itertools.compress('abcd', [1, 0, 1, 0]):"],[20,"\n","24:\"6Bzq\"|36:131"],[20," print (each)"],[20,"\n","24:\"CAdb\"|36:131"],[20,"a"],[20,"\n","24:\"vMei\"|36:131"],[20,"c"],[20,"\n","24:\"32cK\"|36:131"],[20,"'''只判断前三个元素, 并且索引值为 0 和 2 的元素会保留到新迭代器中并返回'''"],[20,"\n","24:\"OKhn\"|36:131"],[20,"temp = itertools.compress(['A', 'B', 'C', 'D'], [1, 0, 1])"],[20,"\n","24:\"z3a3\"|36:131"],[20,"list(temp)"],[20,"\n","24:\"guei\"|36:131"],[20,"# ['A', 'C']"],[20,"\n","24:\"2Nnn\"|36:131"],[20,"4.5 dropwhile","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"GLHK\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"直到predicate为真,就返回iterable剩下的数据, 否则drop掉 ","27:\"10\"|31:2"],[20,"\n","24:\"zGHB\""],[20,"语法:","27:\"10\"|31:2|8:1"],[20,"itertools.dropwhile(predicate, iterable)","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"JJd5\"|7:3"],[20,"for each in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]):"],[20,"\n","24:\"qRkw\"|36:131"],[20," print(each)"],[20,"\n","24:\"JKm9\"|36:131"],[20,"6"],[20,"\n","24:\"BFbA\"|36:131"],[20,"8"],[20,"\n","24:\"gIZh\"|36:131"],[20,"2"],[20,"\n","24:\"vWUY\"|36:131"],[20,"1 "],[20,"\n","24:\"fCNU\"|36:131"],[20,"4.6 filterfalse","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"Y1Ix\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"创建一个迭代器,仅保留 iterable 中在 predicate 计算中为 False 的元素。如果 predicate 传入的是 None,则相当于传入 bool,意思是做真值测试。","27:\"10\""],[20,"\n","24:\"59rS\""],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"itertools.filterfalse(predicate, iterable)","0:\"%2337d9f0\"|27:\"10\"|31:2"],[20,"\n","24:\"OKgv\"|7:3"],[20,"predicate: 只需要一个参数的函数","27:\"10\""],[20,"\n","24:\"zWhY\"|27:\"10\"|7:3|bullet-id:\"5OIT\"|bullet:\"ring\""],[20,"iterable: 可迭代对象","27:\"10\""],[20,"\n","24:\"hoqx\"|27:\"10\"|7:3|bullet-id:\"5OIT\"|bullet:\"ring\""],[20," ","27:\"10\""],[20,"\n","24:\"Ddxt\"|7:3"],[20,"'''元素的值减2小于等于0的为True'''"],[20,"\n","24:\"X6Qa\"|36:131"],[20,"temp = itertools.filterfalse(lambda x: x - 2 <= 0, [1, 2, 0, 3, 1])"],[20,"\n","24:\"O2t2\"|36:131"],[20,"print(list(temp))"],[20,"\n","24:\"cbie\"|36:131"],[20,"# [3]"],[20,"\n","24:\"OUMj\"|36:131"],[20,"\n","24:\"83mQ\"|36:131"],[20,"'''真值测试'''"],[20,"\n","24:\"k5qo\"|36:131"],[20,"temp = itertools.filterfalse(None, [1, 2, 0, 3, 1])"],[20,"\n","24:\"uP0S\"|36:131"],[20,"print(list(temp))"],[20,"\n","24:\"c6Kt\"|36:131"],[20,"# [0]"],[20,"\n","24:\"TQzi\"|36:131"],[20," ","27:\"10\""],[20,"\n","24:\"o0G4\"|32:2|7:3"],[20,"4.7 groupby","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"XO55\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"返回一组(key,itera),key为iterable的值,itera为等于key的所有项","27:\"10\"|31:2"],[20,"\n","24:\"0tLR\""],[20,"语法:itertools.groupby(iterable[,key])","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"278m\"|7:3"],[20,"for key, vale in itertools.groupby('aabbbc'):"],[20,"\n","24:\"5zRm\"|36:131"],[20," print( key, list(vale))"],[20,"\n","24:\"EnmO\"|36:131"],[20,"a ['a', 'a']"],[20,"\n","24:\"1TTK\"|36:131"],[20,"b ['b', 'b', 'b']"],[20,"\n","24:\"3Lga\"|36:131"],[20,"c ['c']"],[20,"\n","24:\"oAt7\"|36:131"],[20,"4.8 islice","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"8O1p\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"相当于迭代器方式的切片操作 ","27:\"10\"|31:2"],[20,"\n","24:\"ZuuR\""],[20,"语法:itertools.islice(iterable, start,stop[,step])","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"WiDy\"|7:3"],[20,"for each in itertools.islice('abcdefg', 1, 4, 2):"],[20,"\n","24:\"wLW4\"|36:131"],[20," print(each) "],[20,"\n","24:\"17FL\"|36:131"],[20,"b"],[20,"\n","24:\"nyME\"|36:131"],[20,"d "],[20,"\n","24:\"eIKk\"|36:131"],[20,"4.9 ","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"starmap()","0:\"%23ff0000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"1DLP\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"返回function(iter)的值,iter为iterable的元素 ","27:\"10\"|31:2"],[20,"\n","24:\"OsNf\""],[20,"语法:itertools.starmap(function,iterable)","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"tegR\"|7:3"],[20,"for each in itertools.starmap(lambda x, y: x * y, [(1, 2), (3, 4)]):"],[20,"\n","24:\"3GEB\"|36:131"],[20," print(each) "],[20,"\n","24:\"jMh2\"|36:131"],[20,"2"],[20,"\n","24:\"z4TN\"|36:131"],[20,"12"],[20,"\n","24:\"x4xD\"|36:131"],[20,"4.10 ","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"takewhile()","0:\"%23ff0000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"ziF3\"|32:2|7:3"],[20,"作用:","27:\"10\"|31:2|8:1"],[20,"创建一个迭代器,将 iterable 中的元素当作 function 的参数计算,与 dropwhile() 函数恰恰相反,当结果的布尔值为 True 时,其元素添加到新迭代器中, 直到有元素的计算结果为 False 时,此元素与之后的元素全部抛弃。","27:\"10\""],[20,"\n","24:\"Kuz3\""],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|31:2|8:1"],[20,"itertools.takewhile(predicate,iterable)","0:\"%2337d9f0\"|27:\"10\"|31:2"],[20,"\n","24:\"SkEH\"|7:3"],[20,"for each in itertools.takewhile(lambda x: x < 5, [1, 3, 5, 6]):"],[20,"\n","24:\"Hw9d\"|36:131"],[20,"print(each)"],[20,"\n","24:\"UrXj\"|36:131"],[20,"1"],[20,"\n","24:\"Wase\"|36:131"],[20,"3 "],[20,"\n","24:\"71pt\"|36:131"],[20," ","27:\"10\""],[20,"\n","24:\"jUIp\"|7:3"],[20,"4.11 tee","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"\n","24:\"Jvps\"|32:2|7:3"],[20,"作用:","27:\"10\"|8:1"],[20,"从一个可迭代对象中分裂出 n 个独立的可迭代对象并返回。","27:\"10\""],[20,"\n","24:\"60ty\""],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|8:1"],[20,"itertools.tee(iterable, n=2)","0:\"%2337d9f0\"|27:\"10\""],[20,"\n","24:\"bZwo\""],[20,"for i in itertools.tee([1, 2, 3, 4, 5, 6], 2):"],[20,"\n","24:\"dtwU\"|36:131"],[20," print(list(i))"],[20,"\n","24:\"EhO1\"|36:131"],[20,"# [1, 2, 3, 4, 5, 6]"],[20,"\n","24:\"J7Dr\"|36:131"],[20,"# [1, 2, 3, 4, 5, 6]"],[20,"\n","24:\"2q5p\"|36:131"],[20," ","27:\"10\""],[20,"\n","24:\"0GH0\"|32:2|7:3"],[20,"4.12 ","0:\"%23ff0000\"|27:\"10\"|31:2"],[20,"zip_longest","0:\"%23ff0000\"|27:\"10\""],[20,"\n","24:\"GLui\"|32:2"],[20,"作用:","27:\"10\"|8:1"],[20,"创建一个迭代器,从所有传入的可迭代对象中抽取一个元素,将它们组合成一个元组,然后添加到迭代器中。","0:\"%2324292e\"|27:\"10\"|31:2"],[20,"\n","24:\"XmF5\""],[20,"语法:","0:\"%2337d9f0\"|27:\"10\"|8:1"],[20,"itertools.zip_longest(*iterables, fillvalue=None)","0:\"%2337d9f0\"|27:\"10\""],[20,"\n","24:\"MdGK\""],[20,"iterables: 一个或多个可迭代对象","27:\"10\""],[20,"\n","24:\"9N1j\"|27:\"10\"|7:3|bullet-id:\"LJW0\"|bullet:\"ring\""],[20,"fillvalue: 关键字参数, 填充值, 默认为 None ","27:\"10\""],[20,"\n","24:\"98E8\"|27:\"10\"|7:3|bullet-id:\"LJW0\"|bullet:\"ring\""],[20,"list(itertools.zip_longest('xyz', '123456', '小伍哥', fillvalue='*'))"],[20,"\n","24:\"b9lT\"|36:131"],[20,"[('x', '1', '小'),"],[20,"\n","24:\"DBp1\"|36:131"],[20," ('y', '2', '伍'),"],[20,"\n","24:\"MREn\"|36:131"],[20," ('z', '3', '哥'),"],[20,"\n","24:\"1isB\"|36:131"],[20," ('*', '4', '*'),"],[20,"\n","24:\"LFlG\"|36:131"],[20," ('*', '5', '*'),"],[20,"\n","24:\"xwmG\"|36:131"],[20," ('*', '6', '*')]"],[20,"\n","24:\"8db7\"|36:131"],[20," ","27:\"10\""],[20,"\n","24:\"jIBc\"|7:3"],[20,"五、总 结","0:\"%23000000\"|27:\"10\"|31:2|8:1"],[20,"\n","24:\"DG1K\"|32:1"],[20,"本文对itertools函数库中的函数进行了分析,并与常用方法做了对比,看得出来,如果能恰当应用itertools中的迭代器函数,会使我们的代码显得更加具备Python风格。","27:\"10\""],[20,"\n","24:\"PYHu\""],[20,"\n","24:\"Lu92\""],[20,"迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(list)来说,迭代器最大的优势就是延迟计算,按需使用,从而提高开发体验和运行效率,以至于在Python 3中map,filter等操作返回的不再是列表而是迭代器。而通过iter函数把列表对象转化为迭代器对象又有点多此一举,这时候我们今天的主角itertools就该上场了。","0:\"%23121212\"|27:\"10\"|31:2"],[20,"\n","24:\"a3Lj\""],[20,"\n","24:\"fFsu\""],[20,"itertools中的函数大多是返回各种迭代器对象,其中很多函数的作用我们平时要写很多代码才能达到,而在运行效率上反而更低,毕竟人家是系统库","0:\"%23121212\"|27:\"10\"|31:2"],[20,"\n\n"],[20,"大概就总结到这里,不过老实说Python的各种语言特性和库还是要多用才能熟练,最终达到随手拈来的程度,装逼的说就是由术入道。","0:\"%23121212\"|27:\"10\"|31:2"],[20,"\n","24:\"dZz2%2CstC8%2CO8x0\""],[20,"\n","24:\"XS4R\""],[20,"\n","24:\"aCtC\""],[20,"\n","24:\"XSCR\""],[20,"六、参考文档","27:\"10\""],[20,"\n","24:\"WX42\"|32:1"],[20,"itertools -- 高效迭代器│Python标准库","27:\"10\"|31:2"],[20,"\n","24:\"XYca\""],[20,"https://mp.weixin.qq.com/s/IGXphM70I9h4ayCn5nmFQA","16:\"https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FIGXphM70I9h4ayCn5nmFQA\"|27:\"10\""],[20,"\n","24:\"mmaY\""],[20,"\n","24:\"aUhj\""],[20,"python标准库系列教程(一)——itertools","27:\"10\""],[20,"\n","24:\"Q1Af\""],[20,"https://mp.weixin.qq.com/s/U3UsIOFZ5iQRJlPSSNh0mQ","16:\"https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FU3UsIOFZ5iQRJlPSSNh0mQ\"|27:\"10\""],[20,"\n","24:\"pRyT\""],[20,"\n","24:\"sHXx\""],[20,"Python标准库系列教程——functools","27:\"10\"|31:2"],[20,"\n","24:\"mafX\""],[20,"https://mp.weixin.qq.com/s/WeOYKMKVnQ6JTjIJZ-1pxA","16:\"https%3A%2F%2Fmp.weixin.qq.com%2Fs%2FWeOYKMKVnQ6JTjIJZ-1pxA\"|27:\"10\""],[20,"\n","24:\"cUAc\""],[20," ","27:\"10\"|31:2"]]">
本文目录
一、模块概述
二、组合生成器
2.1 product
2.2 permutations
2.3 combinations
2.4 combinations_with_replacement
三、无限迭代器
3.1 count
3.2 cycle
3.3 repeat
四、有限迭代器
4.1 accumulate
4.2 chain
4.3 chain.from_iterable
4.4 compress
4.5 dropwhile
4.6 filterfalse
4.7 groupby
4.8 islice
4.9 starmap
4.10 takewhile
4.11 tee
4.12 zip_longest
五、总结
一、模块概述
Functional tools for creating and using iterators,为高效循环而创建迭代器的函数。本模块标准化了一个快速、高效利用内存的核心工具集,这些工具本身或组合都很有用。它们一起形成了“迭代器代数”,这使得在纯Python中有可能创建简洁又高效的专用工具。
permutations函数可以求序列的排列,combinations函数可以求序列的组合,除了这两个函数外,itertools还有相当多的功能,它主要是提供迭代类的操作。
迭代器的特点是:惰性求值(Lazy evaluation),即只有当迭代至某个值时,它才会被计算,这个特点使得迭代器特别适合于遍历大文件或无限集合等,因为我们不用一次性将它们存储在内存中。itertools 模块提供的迭代器函数有以下几种类型:
组合生成器:序列的排列、组合,求序列的笛卡儿积等,比如product可以生成两个序列的笛卡尔积;
无限迭代器:生成一个无限序列,比如count函数能生成自然数序列 1, 2, 3, 4, ...
有限迭代器:接收一个或多个序列(sequence)作为参数,进行组合、分组和过滤等,比如chain函数,能组合多个序列
print(dir(itertools))
['__doc__', '__loader__', '__name__', '__package__', '__spec__',
'_grouper', '_tee', '_tee_dataobject', 'accumulate', 'chain',
'combinations', 'combinations_with_replacement', 'compress',
'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice',
'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee',
'zip_longest']
总之该模块为Python为创建自定义迭代器提供了非常好用的模块——itertools。由itertools提供的工具通常都很快速而且节省内存。你可以利用这些组成部分去创建自己独有的迭代器以完成高效的循环
官方参考文档:https://docs.python.org/zh-cn/3/library/itertools.html
二、排列组合迭代器
迭代器 | 实参 | 结果 |
product() | p, q, ... [repeat=1] | 笛卡尔积,相当于嵌套的for循环 |
permutations() | p[, r] | 长度r元组,所有可能的排列,无重复元素 |
combinations() | p, r | 长度r元组,有序,无重复元素 |
combinations_with_replacement() | p, r | 长度r元组,有序,元素可重复 |
例子 | 结果 |
product('ABCD', repeat=2) | AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD |
permutations('ABCD', 2) | AB AC AD BA BC BD CA CB CD DA DB DC |
combinations('ABCD', 2) | AB AC AD BC BD CD |
combinations_with_replacement('ABCD', 2) | AA AB AC AD BB BC BD CC CD DD |
2.1 product
作用:用于求多个可迭代对象的笛卡尔积,它跟嵌套的 for 循环等价
语法:itertools.product(iter1, iter2, ... iterN, [repeat=1])
for each in itertools.product('ABCD', 'XY'):
print(each)
('A', 'X')
('A', 'Y')
('B', 'X')
('B', 'Y')
('C', 'X')
('C', 'Y')
('D', 'X')
('D', 'Y')
for each in itertools.product('abc', repeat=2):
print (each)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'b')
('b', 'c')
('c', 'a')
('c', 'b')
('c', 'c')
2.2 permutations
作用:返回长度为r的排列
语法:itertools.permutations(iteravle[,r])
iterable: 可迭代对象
r: 关键字参数, 新元素的长度, 默认为 None, 即为新元素的长度就是元素个数
将 iterable 中的元素以长度为 r 进行排列。每个排列组合生成一个元组并添加到新迭代器中。排列时,排列顺序是按照 iterable 中元素的顺序进行的。因此,未排序的 iterable 和已排序的 iterable 排列后的结果是不同的。
若 iterable 中有相同值的元素,但是它们的位置是不同的,排列时也就会会认为它们是不同的。注意: 排列即为数学上的排列,从 n 个元素中取出 m 个元素的无重复排列或直线排列。
for each in itertools.permutations('abc', 2):
print (each)
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
2.3 combinations
作用:返回指定长度的组合
语法: itertools.combinations(iterable, r)
iterable: 可迭代对象
r: 新元素的长度
for each in itertools.combinations('abc', 2):
print (each)
('a', 'b')
('a', 'c')
('b', 'c')
将 iterable 中的元素以长度为 r 进行组合。每个排列组合生成一个元组并添加到新迭代器中。与 permutations() 函数基本一致,但是 combinations() 函数会过滤掉元素值一致的元组。
'''相比于排列,组合会筛选掉元素值一样的元组'''
list(itertools.combinations('ABC', 2))
[('A', 'B'), ('A', 'C'), ('B', 'C')]
'''排列对照组'''
list(itertools.permutations('ABC', 2))
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
2.4 combinations_with_replacement
作用:返回指定长度的组合,组合内元素可重复
语法:itertools.combinations_with_replacement(iterable, r)
iterable: 可迭代对象
r: 关键字参数, 新元素的长度, 默认为 None, 即为新元素的长度就是元素个数
for each in itertools.combinations_with_replacement('abc', 2):
print (each)
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
三、无限迭代器
有三个无限迭代器,当你使用它们的时候,你要知道何时去跳出这个循环,不然你将陷入无限循环之中,在生产数字或者遍历事先不知道长度的数据时非常有用。
迭代器 | 实参 | 结果 | 示例 |
count() | start, [step] | start,start+step,start+2*step, ... | count(10) --> 10 11 12 13 14 ... |
cycle() | p | p0, p1, ... plast, p0, p1, ... | cycle('ABCD') --> A B C D A B C D ... |
repeat() | elem [,n] | elem, elem, elem, ... 重复无限次或n次 | repeat(10, 3) --> 10 10 10 |
3.1 count()
作用:返回以start开始,step递增的序列,无限递增
语法:itertools.count(start=0,step=1)
for each in itertools.count(start=0, step=2):
print (each)
1
2
...
100003
'''小应用, 当取到第三个 A 时停止'''
test_str = ''
for i in itertools.cycle('ABC'):
test_str += i
if test_str.count('A') >= 3:
break
print(test_str)
# ABCABCA
3.2 cycle()
作用:将迭代器进行无限迭代
语法:itertools.cycle(iterable)
for each in itertools.cycle('ab'):
print(each)
a
b
...
b
a
3.3 repeat()
作用:不停的返回object对象,如果指定了times,则返回times次
语法:itertools.repeat(object,[,times])
for each in itertools.repeat('ab', 2):
print(each)
ab
ab
四、有限迭代器
迭代器 | 实参 | 结果 | 示例 |
accumulate() | p [,func] | p0, p0+p1, p0+p1+p2, ... | accumulate([1,2,3,4,5]) --> 1 3 6 10 15 |
chain() | p, q, ... | p0, p1, ... plast, q0, q1, ... | chain('ABC', 'DEF') --> A B C D E F |
chain.from_iterable() | iterable | p0, p1, ... plast, q0, q1, ... | chain.from_iterable(['ABC', 'DEF']) --> A B C D E F |
compress() | data, selectors | (d[0] if s[0]), (d[1] if s[1]), ... | compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F |
dropwhile() | pred, seq | seq[n], seq[n+1], ... 从pred首次真值测试失败开始 | dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 |
filterfalse() | pred, seq | seq中pred(x)为假值的元素,x是seq中的元素。 | filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 |
groupby() | iterable[, key] | 根据key(v)值分组的迭代器 | |
islice() | seq, [start,] stop [, step] | seq[start:stop:step]中的元素 | islice('ABCDEFG', 2, None) --> C D E F G |
starmap() | func, seq | func(*seq[0]), func(*seq[1]), ... | starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 |
takewhile() | pred, seq | seq[0], seq[1], ..., 直到pred真值测试失败 | takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 |
tee() | it, n | it1, it2, ... itn 将一个迭代器拆分为n个迭代器 | |
zip_longest() | p, q, ... | (p[0], q[0]), (p[1], q[1]), ... | zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
4.1 accumulate()
作用:创建一个迭代器,返回累积汇总值或其他双目运算函数的累积结果值(通过可选的 func 参数指定),,若传入 func,则按照 func 中的方法执行。
语法:itertools.accumulate(iterable[, func, *, initial=None])
iterable: 可迭代对象
func: 带有两个参数的函数, 可选参数,func 参数有几种用法。它可以被设为 min() 最终得到一个最小值,或者设为 max() 最终得到一个最大值,或设为 operator.mul() 最终得到一个乘积。摊销表可通过累加利息和支付款项得到。给iterable设置初始值并只将参数 func 设为累加总数可以对一阶 递归关系 建模。
initial: 关键字参数, 默认为 None, 若此参数传参, 则此参数作为 iterable 的第一个元素
'''默认情况, 返回累计汇总值'''
list(itertools.accumulate([1, 2, 3, 4, 5]))
[1, 3, 6, 10, 15]
'''方法修改为计算累计的乘积'''
import operator #该模块输出一系列对应Python内部操作符的函数
data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
list(itertools.accumulate(data, operator.mul))
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
'''方法修改为计算累计的最大值'''
list(itertools.accumulate(data, max))
[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(itertools.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']
'''在迭代器起始位置添加一个元素, 方法不变, 返回累计汇总值'''
list(itertools.accumulate([1, 2, 3, 4, 5], initial=100))
# [100, 101, 103, 106, 110, 115]
'''在迭代器起始位置添加一个元素, 方法修改为计算累计的乘积'''
list(itertools.accumulate([1, 2, 3, 4, 5], lambda x, y: x * y, initial=10))
#[10, 10, 20, 60, 240, 1200]
4.2 chain()
作用:chain 接收多个可迭代对象作为参数,将它们『连接』起来,作为一个新的迭代器返回。
语法:chain(iterable1, iterable2, iterable3, ...)
list(itertools.chain([1, 2, 3], ('A', 'B', 'C'), {'一', '二', '三'}))
[1, 2, 3, 'A', 'B', 'C', '二', '一', '三']
list(itertools.chain( 'ABC', 'DEF'))
['A', 'B', 'C', 'D', 'E', 'F']
4.3 chain.from_iterable
作用:与 itertools.chain() 函数类似,但是参数是一个可迭代对象,将这个可迭代对象中元素一一添加到新迭代器中,如果元素是一个可迭代对象,那么会将这个元素内的元素一一添加到新迭代器中。小编自己的理解就是迭代器降维。
语法:itertools.chain.from_iterable(iterable)
'''将二维迭代器降维'''
temp = itertools.chain.from_iterable(['1', ['2', '3'], ('4', '5')])
print(list(temp))
# ['1', '2', '3', '4', '5']
'''只能降一层维度, 三维将至二维'''
temp = itertools.chain.from_iterable(['1', ['2', '3'], ('4', ['5', '6'])])
print(list(temp))
# ['1', '2', '3', '4', ['5', '6']]
PS: 迭代器维度概念可以理解为,整个迭代器中的元素都不是迭代器类型的就是一维,迭代器中的元素有是迭代器的就是二维,迭代器中的元素有是迭代器的,然后这个迭代器中还有元素是迭代器的就是三维,依此类推。
4.4 compress
作用:创建一个迭代器,将 data 中经过 selectors 真值测试为 True 的元素保留。当两个可迭代对象中的某一个到达末尾时执行停止,返回最终结果。
语法:compress(data, selectors)
data: 可迭代对象, 包含所需的数据
selectors: 可迭代对象, 真值测试数据
for each in itertools.compress('abcd', [1, 0, 1, 0]):
print (each)
a
c
'''只判断前三个元素, 并且索引值为 0 和 2 的元素会保留到新迭代器中并返回'''
temp = itertools.compress(['A', 'B', 'C', 'D'], [1, 0, 1])
list(temp)
# ['A', 'C']
4.5 dropwhile
作用:直到predicate为真,就返回iterable剩下的数据, 否则drop掉
语法:itertools.dropwhile(predicate, iterable)
for each in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]):
print(each)
6
8
2
1
4.6 filterfalse
作用:创建一个迭代器,仅保留 iterable 中在 predicate 计算中为 False 的元素。如果 predicate 传入的是 None,则相当于传入 bool,意思是做真值测试。
语法:itertools.filterfalse(predicate, iterable)
predicate: 只需要一个参数的函数
iterable: 可迭代对象
'''元素的值减2小于等于0的为True'''
temp = itertools.filterfalse(lambda x: x - 2 <= 0, [1, 2, 0, 3, 1])
print(list(temp))
# [3]
'''真值测试'''
temp = itertools.filterfalse(None, [1, 2, 0, 3, 1])
print(list(temp))
# [0]
4.7 groupby
作用:返回一组(key,itera),key为iterable的值,itera为等于key的所有项
语法:itertools.groupby(iterable[,key])
for key, vale in itertools.groupby('aabbbc'):
print( key, list(vale))
a ['a', 'a']
b ['b', 'b', 'b']
c ['c']
4.8 islice
作用:相当于迭代器方式的切片操作
语法:itertools.islice(iterable, start,stop[,step])
for each in itertools.islice('abcdefg', 1, 4, 2):
print(each)
b
d
4.9 starmap()
作用:返回function(iter)的值,iter为iterable的元素
语法:itertools.starmap(function,iterable)
for each in itertools.starmap(lambda x, y: x * y, [(1, 2), (3, 4)]):
print(each)
2
12
4.10 takewhile()
作用:创建一个迭代器,将 iterable 中的元素当作 function 的参数计算,与 dropwhile() 函数恰恰相反,当结果的布尔值为 True 时,其元素添加到新迭代器中, 直到有元素的计算结果为 False 时,此元素与之后的元素全部抛弃。
语法:itertools.takewhile(predicate,iterable)
for each in itertools.takewhile(lambda x: x < 5, [1, 3, 5, 6]):
print(each)
1
3
4.11 tee
作用:从一个可迭代对象中分裂出 n 个独立的可迭代对象并返回。
语法:itertools.tee(iterable, n=2)
for i in itertools.tee([1, 2, 3, 4, 5, 6], 2):
print(list(i))
# [1, 2, 3, 4, 5, 6]
# [1, 2, 3, 4, 5, 6]
4.12 zip_longest
作用:创建一个迭代器,从所有传入的可迭代对象中抽取一个元素,将它们组合成一个元组,然后添加到迭代器中。
语法:itertools.zip_longest(*iterables, fillvalue=None)
iterables: 一个或多个可迭代对象
fillvalue: 关键字参数, 填充值, 默认为 None
list(itertools.zip_longest('xyz', '123456', '小伍哥', fillvalue='*'))
[('x', '1', '小'),
('y', '2', '伍'),
('z', '3', '哥'),
('*', '4', '*'),
('*', '5', '*'),
('*', '6', '*')]
五、总 结
迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(list)来说,迭代器最大的优势就是延迟计算,按需使用,从而提高开发体验和运行效率,以至于在Python 3中map,filter等操作返回的不再是列表而是迭代器。而通过iter函数把列表对象转化为迭代器对象又有点多此一举,这时候我们今天的主角itertools就该上场了。
itertools中的函数大多是返回各种迭代器对象,其中很多函数的作用我们平时要写很多代码才能达到,而在运行效率上反而更低,大概就总结到这里,不过Python的各种语言特性和库还是要多用才能熟练,最终达到随手拈来的程度。
推荐阅读:
万字长文详解|Python库collections,让你击败99%的Pythoner
↓扫描关注本号↓