Summary of common methods of Pythons itertools package
編輯:Python
import itertools
for i in itertools.chain([1,2],[3,4]):# Splice the list and iterate
print(i)
for i in itertools.chain((1,2),{
3:1,'a':3}):# list 、 Tuples 、 aggregate 、 Dictionaries can be combined at will
print(i)
sum=0
for i in itertools.cycle([1,2]):# Infinite loop iteration , But only one parameter can be passed here
print(i)
if sum>2:
break
sum+=1
for i in itertools.accumulate([1,2,3]):# Calculate the cumulative sum , Only one parameter can be passed here
print(i)
for i in itertools.accumulate(['a','b','c']):# Calculate the cumulative sum , Strings can also be spliced
print(i)
for i in itertools.accumulate([1,2,3],lambda a,b:a*b):# You can pass in custom functions , Calculate the accumulation here
print(i)