Fichas
python iterables and iteration

Fichas del conjunto (35)
- 1create a list, for multiplying each element by 2 , in the range(10), using comprehensionlst = [ i * 2 for i in range(10)]
- 2create a list of sets using comprehension: [(0,0) , (0,1),...(4,2)]lst= [ (x,y) for x in range(5) for y in range(3) ]
- 3create a list of numbers 1-10, excluding x=5 using comprehensionlst = [ x for x in range(1,10) if x!=5 ]
- 4formalate comprehnsion: [ (x,y) for x in range(10) for y in range(x) ] as for loopsresult = []
for x in range(10):
for y in range(x):
result.append((x, y)) - 5can you have nested comprehension?yes
- 6create [[], [0], [0, 3], [0, 3, 6]] (range is 0-4, multiplied * 3) using nested comprehensionsvals =
[[y*3 for y in range(x)] for x in range(4)] - 7what does map() do?applies a function to every element in a sequence, producing a new sequence
- 8how do you use map to find ord of every character in 'the fox'x=list(map(ord, 'the fox'))
- 9what does map(ord, 'the fox') return?
- 10why does map return ?it uses lazy evaluation
- 11how do you solve lazy evaluation of map easily?deconstruct it immediately:
list(map(ord, 'the fox')) - 12how many input sequences can map() accept?any number, as long as number of inputs match number of function arguments
- 13what does map architcture look like for 3 seqences sent into a function that takes 3 input parameters

- 14sizes=['s','m','l'] colors=['blue','red','yel'] combine(size,color) map the function above using map()list(map(combine, sizes, colors))
- 15sizes=['s','m','l'] colors=['blue','red','yel'] combine(size,color) lst= list(map(combine, sizes, colors)) what is lst?lst=['s blue', 'm red', 'l yel']
- 16if input sequences are not same size, when does map stop?when shortest sequence is exhausted
- 17what gives a new seqence of all true return values for a function for each element in a sequencefilter()
- 18given a function is_odd(num) x=[1,2,3,4,5,6] how would you filter x?filter (is_odd, x)
- 19x=[1,2,3,-4,-5,6] create a filter of all positive values of xpositives = filter(lambda ps: ps0, x)
- 20is filter lazy ?yes, has to be iterated
- 21how do you remove all elements in which evaluate to false (using filter)pass None as first argument to filter
- 22what is f in: trues = filter(None, [0,1,False,True]) f=list(trues)[1, True]
- 23how do you repeatedly reply a function to elements and reduce them to a valuefunctools.reduce()
- 24how would you reduce() f=[1,2,3,4,5] to sum them upfrom functools import reduce
import operator
reduce(operator.add, f) - 25how do you set initial value of reduce()?set optional initial value, ie:
values=[1,2,3]
reduce(operator.add, values, 0) - 26what should the initial value for reduce be when using operator.mul ?1
- 27how do you create an iterator?iter()
- 28how do you get next element in sequence?next()
- 29what signals the end of the iteration sequence?StopIteration
- 30what is an iterable ?an object that implements the __iter__() method
- 31what is an iterator?an object that implements the iterable protocol
and implements the __next__() method - 32when creating the code for __next__() method, how do you signal end of data?raise StopIteration()
- 33whats an alternative way to implement iterable protocol, supporting consecutive integer indexing?code __getitem__()
- 34what would the __get_item() return?def __getitem__(self, idx):
return self.data[idx] - 35when using iter(callable, sentinal) when would loop iter stop?when callable produces value equal to sentinal
Información
- Descripción
- (beyond basics python flashcards) python Fichas sobre python iterables and iteration, creado por Andrew Sperazza el 11/01/2017.
- Creado por
- Andrew Sperazza
- Visitas
- 2
- Etiquetas
- Valoración