from itertools import count
for num in count(3,-2):
print(num)
from itertools import cycle
ITER1 = [1,2,3,4,5]
ITER2 = (1,2,3,4,5)
cycle1 = cycle(ITER1)
cycle2 = cycle(ITER2)
for _ in range(5):
print(next(cycle1))
print(next(cycle2))
>>> repeat(10, 3)
repeat(10, 3)
>>> repeat(10, 3).__str__
>>> print(repeat(10, 3))
repeat(10, 3)
>>> type(repeat(10, 3))
from itertools import repeat
for num in repeat(10, 3):
print(num)
from itertools import repeat
from itertools import count
from itertools import cycle
print(type(repeat(10, 3)))
print(type(count(10, 3)))
print(type(cycle([10, ])))
from itertools import chain
iter1 = [1, 2, 3]
iter2 = "string"
iter3 = (111,222,333)
iter4 = {"1111":1111,"2222":2222,"3333":3333}
combined = chain(iter1,iter2,iter3,iter4)
for item in combined:
print(item)
from itertools import compress
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
selectors = [True, True, True, False, True, True, False, True, False, True]
filtered = compress(data, selectors)
for item in filtered:
print(item)
from itertools import dropwhile
data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
def fun(x):
return (x+1)**x < 300
droped = dropwhile(fun, data)
for iter_term in droped:
print(iter_term)
from itertools import takewhile
data = [1, 1.3, 1.5, 7, 2.9, 1.2, 1.4, 3.6, 1.8, 1.0]
def fun(x):
return (x+1)**x < 300
taked = takewhile(fun, data)
for iter_term in taked:
print(iter_term)
from itertools import permutations
data = [1,2,3,4,5]
result = permutations(data,2)
for iter_term in result:
print(iter_term)
from itertools import combinations
data = [1,2,3,4,5]
result = combinations(data,2)
for iter_term in result:
print(iter_term)
from itertools import product
numbers = [1, 2, 3]
capital = ['A', 'B', 'C']
lowercase = ['a', 'b', 'c']
result = product(numbers, capital, lowercase)
for iter_term in result:
print(iter_term)
>>> k = itertools.product
>>> lk = k(["1","2","3","4"],3)
Traceback (most recent call last):
File "", line 1, in
lk = k(["1","2","3","4"],3)
TypeError: 'int' object is not iterable
>>> lk = k(["1","2","3","4"],repeat=3)
>>> lk
>>> list(lk)
[('1', '1', '1'), ('1', '1', '2'), ('1', '1', '3'), ('1', '1', '4'), ('1', '2', '1'), ('1', '2', '2'), ('1', '2', '3'), ('1', '2', '4'), ('1', '3', '1'), ('1', '3', '2'), ('1', '3', '3'), ('1', '3', '4'), ('1', '4', '1'), ('1', '4', '2'), ('1', '4', '3'), ('1', '4', '4'), ('2', '1', '1'), ('2', '1', '2'), ('2', '1', '3'), ('2', '1', '4'), ('2', '2', '1'), ('2', '2', '2'), ('2', '2', '3'), ('2', '2', '4'), ('2', '3', '1'), ('2', '3', '2'), ('2', '3', '3'), ('2', '3', '4'), ('2', '4', '1'), ('2', '4', '2'), ('2', '4', '3'), ('2', '4', '4'), ('3', '1', '1'), ('3', '1', '2'), ('3', '1', '3'), ('3', '1', '4'), ('3', '2', '1'), ('3', '2', '2'), ('3', '2', '3'), ('3', '2', '4'), ('3', '3', '1'), ('3', '3', '2'), ('3', '3', '3'), ('3', '3', '4'), ('3', '4', '1'), ('3', '4', '2'), ('3', '4', '3'), ('3', '4', '4'), ('4', '1', '1'), ('4', '1', '2'), ('4', '1', '3'), ('4', '1', '4'), ('4', '2', '1'), ('4', '2', '2'), ('4', '2', '3'), ('4', '2', '4'), ('4', '3', '1'), ('4', '3', '2'), ('4', '3', '3'), ('4', '3', '4'), ('4', '4', '1'), ('4', '4', '2'), ('4', '4', '3'), ('4', '4', '4')]
>>>
>>> f = itertools.combinations_with_replacement
>>> f(["1","2","3","4"],3)
>>> lf = f(["1","2","3","4"],3)
>>> list(lf)
[('1', '1', '1'), ('1', '1', '2'), ('1', '1', '3'), ('1', '1', '4'), ('1', '2', '2'), ('1', '2', '3'), ('1', '2', '4'), ('1', '3', '3'), ('1', '3', '4'), ('1', '4', '4'), ('2', '2', '2'), ('2', '2', '3'), ('2', '2', '4'), ('2', '3', '3'), ('2', '3', '4'), ('2', '4', '4'), ('3', '3', '3'), ('3', '3', '4'), ('3', '4', '4'), ('4', '4', '4')]
>>> counter1
Counter({1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1})
>>> counter2
Counter({6: 3, 5: 2, 2: 1, 7: 1, 8: 1})
from collections import Counter
set1 = set([1, 2, 3, 4, 4, 5, 6, 6])
list2 = [2, 5, 5, 6, 6, 6, 7, 8]
counter1 = Counter(set1)
counter2 = Counter(list2)
intersection = counter1 & counter2
print("Intersection:", intersection.keys())
union = counter1 | counter2
print("Union:", union.keys())
difference = counter1 - counter2
print("Difference1:", difference.keys())
difference = counter2 - counter1
print("Difference2:", difference.keys())
>>> counter2.most_common
>>> counter2.most_common[0]
Traceback (most recent call last):
File "
counter2.most_common[0]
TypeError: 'method' object is not subscriptable
>>>
from collections import Counter
counter1 = Counter(a=3, b=2, c=1)
counter2 = Counter(a=1, b=2, c=3)
counter1.subtract(counter2)
print(counter1)
print(counter1 - counter2)
Counter({'a': 2, 'b': 0, 'c': -2})
Counter({'a': 1})
from collections import Counter
counter1 = Counter(a=3, b=2, c=1)
counter3 = Counter(a=1, d=2, c=3)
counter1.subtract(counter3)
print(counter1)
print(counter1 - counter3)