This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import compress | |
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] | |
selector = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] | |
for i in compress(data, selector): | |
print(i, end=" ") | |
# OUTPUT | |
# Simply prints all numbers for which the corresponding boolean value in selector | |
#is True, in this case all the even numbers upto 10. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain | |
for i in chain([0,1,2,3], range(4,11), 'ABC'): | |
print(i, end = ' ') | |
#OUTPUT | |
''' | |
0 1 2 3 4 5 6 7 8 9 10 A B C | |
''' | |
my_iterable = [[0,1,2],[3,4],[5,6,7,8,9,10],['ABC']] | |
for i in chain.from_iterable(my_iterable): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import accumulate | |
data = [-1, 2, 5, 7, -20, 9, 12, 9, 16, 4] | |
print(list(accumulate(data, min))) | |
#Prints the running minimum of the data | |
#OUTPUT | |
''' | |
[-1, -1, -1, -1, -20, -20, -20, -20, -20, -20] | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import accumulate | |
from operator import mul | |
iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
print(list(accumulate(iterable))) | |
# Prints the addition after each iteration | |
# OUTPUT | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import repeat | |
print(list(map(pow, range(10), repeat(2)))) | |
#Prints the square of all numbers in range(10) | |
#OUTPUT | |
''' | |
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import repeat | |
for i in repeat(1,5): | |
print(i, end = ' ') | |
#OUTPUT | |
''' | |
1 1 1 1 1 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import cycle | |
count = 0 | |
iterable = ["Python", "is", "awesome."] | |
generator = cycle(iterable) | |
for i in range(6): | |
print(next(generator, end = ' ') | |
# OUTPUT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import cycle | |
count = 0 | |
for i in cycle('ABC'): | |
print(i, end = ' ') | |
count += 1 | |
if count == 6: | |
break | |
# OUTPUT | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import count | |
count_floating_negative = count(start=0.5, step = -0.5) | |
print(list(next(count_floating_negative) for _ in range(10))) | |
# OUTPUT | |
''' | |
[0.5, 0.0, -0.5, -1.0, -1.5, -2.0, -2.5, -3.0, -3.5, -4.0] | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import count | |
for i in count(0,2): | |
print(i, end = " ") | |
if i==20: | |
break | |
# OUTPUT | |
''' | |
0 2 4 6 8 10 12 14 16 18 20 | |
''' |