PYTHON ADVANCED - 07: ITERTOOLS
HERE, WE WILL LEARN ABOUT ITERTOOLS:
NOTE: TRY ALL METHODS/CODES
#itertools : product, permutations, combinations, accumulate, groupby, and infinite iterators
# from itertools import product
# a = [1,2]
# b =[3]
# c = [5,6]
# prod = product(a,c)
# # prod = product(a,b, repeat=2)
# print(list(prod))
# from itertools import permutations
# a = [1,2,3]
# perm = permutations(a)
# print(list(perm))
# from itertools import combinations, combinations_with_replacement
# a = [1,2,3]
# comb = combinations(a, 2)
# print(list(comb))
# combwr = combinations_with_replacement(a, 2)
# print(list(combwr))
# from itertools import accumulate
# import operator
# a = [1,2,5,3,4]
# # acc = accumulate(a)
# # acc = accumulate(a, func = operator.mul)
# acc = accumulate(a, func = max)
# print(a)
# print(list(acc))
# from itertools import groupby
# def smaller_than_3(x):
# return x<3
# a = [1,2,3,4]
# groupobj = groupby(a, key=smaller_than_3)
# for key,value in groupobj:
# print(key, list(value))
from itertools import count, cycle, repeat
# for i in count(10):
# print(i)
# if i ==15:
# break
# a = [1,2,3]
# for i in cycle(a):
# print(i)
a = [1,2,3]
for i in repeat(1, 4):
print(i)
Comments
Post a Comment