Comprehensions and Generators Flashcards

(31 cards)

1
Q

map(function, iterable, …)

A

Return an iterator that applies function to every item of iterable, yielding the results.
If additional iterable arguments are passed, function must take that many arguments
and is applied to the items from all iterables in parallel.

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

> list(map(lambda *a: a, range(3), ‘abc’))
# 2 iterables

A

[(0, ‘a’), (1, ‘b’), (2, ‘c’)]

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

list(map(lambda *a: a, (1, 2), ‘abc’))
# (1, 2) shortest

A

[(1, ‘a’), (2, ‘b’)]

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

map() stops when the ____ o the iterable we call it with is exhausted

A

shortest

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

zip(*iterables)

A

Returns an iterator of tuples, where the i-th tuple contains the i-th element from
each of the argument sequences or iterables. The iterator stops when the shortest
input iterable is exhausted. With a single iterable argument, it returns an iterator
of 1-tuples. With no arguments, it returns an empty iterator.

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

> > > grades = [18, 23, 30, 27]
avgs = [22, 21, 29, 24]
list(zip(avgs, grades))

A

[(22, 18), (21, 23), (29, 30), (24, 27)]

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

> > > list(map(lambda *a: a, avgs, grades))

A

[(22, 18), (21, 23), (29, 30), (24, 27)]

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

filter(function, iterable)

A

Construct an iterator from those elements of iterable for which function returns
True. iterable may be either a sequence, a container which supports iteration, or an
iterator. If function is None, the identity function is assumed, that is, all elements
of iterable that are false are removed.

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

> > > test = [2, 5, 8, 0, 0, 1, 0]
list(filter(None, test))

A

er(None, test))
[2, 5, 8, 1]

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

> > > list(filter(lambda x: x > 4, test)) # keep only items > 4

A

[5, 8]

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

> > > squares = map(lambda n: n**2, range(10))
list(squares)

A

[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
12
Q

LIST COMPREHENSION
»> [n ** 2 for n in range(10)]

A

[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

filter passes each element in the iterable through func and return only the ones that evaluate to ____

A

true

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

what prints?

sq1 = list(
map(lambda n: n ** 2, filter(lambda n: not n % 2, range(10)))
)
# equivalent, but using list comprehensions
sq2 = [n ** 2 for n in range(10) if not n % 2]
print(sq1, sq1 == sq2)

A

prints: [0, 4, 16, 36, 64] True

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

You can also use nested comprehension in place of ____ loop

A

nested

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

for a in range(len(items)):
for b in range(a, len(items)):
pairs.append((items[a], items[b]))

What is the equivalent nested comprehension

A

items = ‘ABCD’
pairs = [(items[a], items[b])
for a in range(len(items)) for b in range(a, len(items))]

17
Q

We can also apply filtering to a comprehension

Example

A

from math import sqrt
# this will generate all possible pairs
mx = 10

triples = [(a, b, sqrt(a2 + b2))
for a in range(1, mx) for b in range(a, mx)]

this will filter out all non-Pythagorean triples
triples = list(
filter(lambda triple: triple[2].is_integer(), triples))

print(triples) # prints: [(3, 4, 5.0), (6, 8, 10.0)]

18
Q

Another way

mx = 10
triples = [(a, b, sqrt(a2 + b2))
for a in range(1, mx) for b in range(a, mx)]

here we combine filter and map in one CLEAN list comprehension

triples = [(a, b, int(c)) for a, b, c in triples if c.is_integer()]

A

print(triples)
# prints: [(3, 4, 5), (6, 8, 10)]

19
Q

We can also make _____ comprehensions

20
Q

Dictionaries do not allow _____ keys

21
Q

word = ‘Hello’
swaps = {c: c.swapcase() for c in word}
print(swaps)

A

prints: {‘H’: ‘h’, ‘e’: ‘E’, ‘l’: ‘L’, ‘o’: ‘O’}

22
Q

We can also create ____ comprehensions

A

word = ‘Hello’
letters1 = {c for c in word}
letters2 = set(c for c in word)
print(letters1) # prints: {‘H’, ‘o’, ‘e’, ‘l’}
print(letters1 == letters2) # prints: True

23
Q

These are very similar to regular functions but instead of returning results through return statements, they use yield which allows them to suspend and resume their state between each call

A

generator functions

24
Q

These are very similar to list comprehensions, but instead of returning a list, they return an object that produces results one by one

A

generator expressions

25
First example of generators
def get_squares_gen(n): # generator approach for x in range(n): yield x ** 2 # we yield, we don't return print(list(get_squares_gen(10)))
26
So, in order to save memory (and time), use generator functions whenever possible.
True
27
def fun(max): cnt = 1 while cnt <= max: yield cnt cnt += 1 ctr = fun(5) for n in ctr: print(n)
1 2 3 4 5
28
_____ is used in generator functions to provide a sequence of values over time. When _____ is executed, it pauses the function, returns the current value and retains the state of the function.
yield
29
In general, generator expressions behave like equivalent list comprehensions, but there is one very important thing to remember: generators allow for ____ ________ only, then they will be exhausted.
one iteration
30
>>> cubes_gen = (k**3 for k in range(10)) # create as generator >>> cubes_gen at 0x103fb5a98> >>> type(cubes_gen) >>> list(cubes_gen) # this will exhaust the generator [0, 1, 8, 27, 64, 125, 216, 343, 512, 729] >>> list(cubes_gen)
nothing more to give At the end there is nothing more to give as we have looped though the elements from the genearator expression
31
cubes1 = map( lambda n: (n, n**3), filter(lambda n: n % 3 == 0 or n % 5 == 0, range(N)) ) cubes2 = ( (n, n**3) for n in range(N) if n % 3 == 0 or n % 5 == 0)
If you print the list (cubes1), you get: [(0, 0), (3, 27), (5, 125), (6, 216), (9, 729), (10, 1000), (12, 1728), (15, 3375), (18, 5832)].