from itertools import accumulate, takewhile nums = list(accumulate(range(8))) print(nums) print(list(takewhile(lambda x: x<= 6, nums)))
结果
[0, 1, 3, 6, 10, 15, 21, 28] [0, 1, 3, 6] # 解释 [0, 1, 3, 6, 10, 15, 21, 28] 0+0=0 1+0=1 2+1=3 3+3=6 4+6=10 5+10=15 6+15=21 7+21=28 # 解释 [0, 1, 3, 6]