aa
1 #演示itertools.accumulate生成器函数 2 3 import itertools 4 import operator 5 6 7 sample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1] 8 9 e1 = list(itertools.accumulate(sample)) 10 print("默认求累计和:", e1) #[5, 9, 11, 19, 26, 32, 35, 35, 44, 45] 11 12 e2= list(itertools.accumulate(sample, min)) 13 print("传递min函数,求累计结果:", e2) #[5, 4, 2, 2, 2, 2, 2, 0, 0, 0] 14 15 e3 = list(itertools.accumulate(sample, max)) 16 print("传递max函数,求累计结果:", e3) #[5, 5, 5, 8, 8, 8, 8, 8, 9, 9] 17 18 19 e4 = list(itertools.accumulate(sample, operator.mul)) 20 print("传递operator.mul函数,求累计结果:", e4) #[5, 20, 40, 320, 2240, 13440, 40320, 0, 0, 0] 21 22 e5 = list(itertools.accumulate(range(1,11), operator.mul)) 23 print("累计结果:", e5) # [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
1
import itertools import operator a = list(enumerate('albatroz', start = 1)) print(a) #[(1, 'a'), (2, 'l'), (3, 'b'), (4, 'a'), (5, 't'), (6, 'r'), (7, 'o'), (8, 'z')] a1 =list(enumerate('albatroz', start =4)) print(a1) #[(4, 'a'), (5, 'l'), (6, 'b'), (7, 'a'), (8, 't'), (9, 'r'), (10, 'o'), (11, 'z')] a3 = list(map(operator.mul, range(11), range(11))) print(a3) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] a4 = list(map(operator.mul, range(11), [2, 4, 8])) print(a4) #[0, 4, 16] a5 = list(map(lambda a, b: (a, b), range(11), [2, 4, 8])) #生成元组对 print(a5) #[(0, 2), (1, 4), (2, 8)] a6 = list(itertools.starmap(operator.mul, enumerate('albatroz', 1))) print(a6) #['a', 'll', 'bbb', 'aaaa', 'ttttt', 'rrrrrr', 'ooooooo', 'zzzzzzzz'] sample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1] a7 = list(itertools.starmap(lambda a, b: b/a, enumerate(itertools.accumulate(sample), 1))) print(list(itertools.accumulate(sample))) #[5, 9, 11, 19, 26, 32, 35, 35, 44, 45] print( list(enumerate(itertools.accumulate(sample), 1))) #[(1, 5), (2, 9), (3, 11), (4, 19), (5, 26), (6, 32), (7, 35), (8, 35), (9, 44), (10, 45)] print(a7) #[5.0, 4.5, 3.6666666666666665, 4.75, 5.2, 5.333333333333333, 5.0, 4.375, 4.888888888888889, 4.5]
ff
标签:11,函数,生成器,list,sample,accumulate,itertools,库中,print From: https://www.cnblogs.com/bravesunforever/p/17417133.html