itertools Flashcards

1
Q

count()

A

an infinite interator

counter = itertools.count()

for num in counter:

if num > 10:

break

print(num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

To use the itertools methods what is required?

A

import itertools

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does this return, if the preceding counter was 12?

print(next(counter)

A

13 or the next value in the counter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this return?

data = [100, 200, 300, 400]

daily_data = list(zip(itertools.count(), data))

daily_data

A

[(0, 100), (1, 200), (2, 300), (3, 400)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does this return?

data = [100, 200, 300, 400]

daily_data = list(enumerate(data))

daily_data

A

the same result as itertools.count()

[(0, 100), (1, 200), (2, 300), (3, 400)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Provide the values returned If given

counter = itertools.count(start=5, step=5)

print(next(counter))

print(next(counter))

print(next(counter))

print(next(counter))

A

5

10

15

20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why won’t this zip method return 10 values?

data = [100, 200, 300, 400]

daily_data = list(zip(range(10), data))

daily_data

[(0, 100), (1, 200), (2, 300), (3, 400)]

A

Because the zip() is exhausted by the data, only four values being inputed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What would be the outcome of this if itertool.zip_longest() was utilized instead of zip()?

data = [100, 200, 300, 400]

daily_data = list(zip(range(10), data))

daily_data

[(0, 100), (1, 200), (2, 300), (3, 400)]

A

Full range would be outputed with None values for those that do not have a subsequent match

[(0, 100), (1, 200), (2, 300), (3, 400), (4, None), (5, None), (6, None), (7, None), (8, None), (9, None)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

cycle()

A

An Infinite iterator

counter = itertools.cycle([1,2,3])

print(next(counter)) #1

print(next(counter)) #2

print(next(counter)) #3

print(next(counter)) #1

print(next(counter)) #2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is this cycle() mimicing?

counter = itertools.cycle((‘On’, ‘Off’))

A

an on off toggle switch, value is either on or off.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

repeat()

A

Infinite iterator

counter = itertools.repeat(2, times= 3)

print(next(counter)) #2

print(next(counter)) #2

print(next(counter)) #2

print(next(counter)) Traceback (most recent call last) StopIteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output?

squares = map(pow, range(10), itertools.repeat(2))

print(list(squares))

A

0-9 squares

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the ouput?

squares = itertools.starmap(pow, [(2,5), (3,2), (10,3)])

A

2 to the 5th = 32

3 squared = 9

10 cubed = 1000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

combinations()

A

Combinatoric iterator

order does not matter

letters = [‘a’, ‘b’, ‘c’, ‘d’]

result = itertools.combinations(letters, 2)

for item in result:

print(item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

permutations()

A

Combinatoric iterator

good for racing events where order matters

letters = [‘a’, ‘b’, ‘c’, ‘d’]

for item in itertools.permutations(letters, 2):

print(item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

product()

A

Combinatoric iterator

equivalent to a nested for-loop, similiar to combination_with_replacement. good for password cracking type app

for item in itertools.product(numbers, repeat=4):

print(item)

17
Q

What does this return?

numbers = [0, 1, 2, 3]

for item in itertools.combinations_with_replacement(numbers, 4):

print(item)

A

(0, 0, 0, 0) (0, 0, 0, 1) (0, 0, 0, 2) (0, 0, 0, 3) (0, 0, 1, 1) (0, 0, 1, 2) (0, 0, 1, 3) (0, 0, 2, 2) (0, 0, 2, 3) (0, 0, 3, 3) (0, 1, 1, 1) (0, 1, 1, 2) (0, 1, 1, 3) (0, 1, 2, 2)

18
Q

chain()

A

Iterator terminating on the shortest input sequence

combines lists and then iterates over them in order

for items in itertools.chain(letters, numbers, names):

print(items)

19
Q

islice()

A

Iterator terminating on the shortest input sequence

elements from seq[start:stop:step]

range of 10 cut by half

for item in itertools.islice(range(10), 5):

print(item)

0
1
2
3
4

20
Q

compress()

A

Iterator terminating on the shortest input sequence

data, selectors

selectors = [True, True, False, True]
letters = ['a', 'b', 'c', 'd']

for item in itertools.compress(letters, selectors):

print(item)

a
b
c

21
Q

filterfalse()

A

Iterator terminating on the shortest input sequence

this values are NOT less than 2
# returns values that are false in the list 
# that was passed (numbers)

for item in itertools.filterfalse(lt_2, numbers):

print(item)

22
Q

dropwhile()

A

Iterator terminating on the shortest input sequence

any values less than 2 will be dropped
# once a True conditon is met it will return 
# rest of the values in the list True or not.

numbers = [0,1,2,3,2,1,0]

for item in itertools.dropwhile(lt_2, numbers):

print(item)

23
Q

takewhile()

A

Iterator terminating on the shortest input sequence

similiar to dropwhile()
# first two values are True 
# breaks from the iterable once a False value is met

for item in itertools.takewhile(lt_2, numbers):

print(item)

24
Q

accumulate()

A

Iterator terminating on the shortest input sequence

running total of the values in the list

print(numbers)

for item in itertools.accumulate(numbers):

print(item)

25
Q

accumulate()

A

Iterator terminating on the shortest input sequence

import operator

running product mul_tiplication

num = [1,2,3,2,1,0]

for item in itertools.accumulate(num, operator.mul):

print(item)

26
Q

tee()

A

Iterator terminating on the shortest input sequence

copies an iterator
# don't use original once copies are made!

copy1, copy2 = itertools.tee(person_group)

27
Q

groupby()

A

Iterator terminating on the shortest input sequence

people, state in dict object

for key, group in itertools.groupby(people, get_state):

print(key)

for person in group:

print(person)

print()