O GoConqr está de visual novo! Conte para nós o que você achou.

Flashcards

python iterables and iteration

Andrew Sperazza python Inglês 35 flashcards

python iterables and iteration

Flashcards do conjunto (35)

  1. 1create a list, for multiplying each element by 2 , in the range(10), using comprehensionlst = [ i * 2 for i in range(10)]
  2. 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) ]
  3. 3create a list of numbers 1-10, excluding x=5 using comprehensionlst = [ x for x in range(1,10) if x!=5 ]
  4. 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))
  5. 5can you have nested comprehension?yes
  6. 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)]
  7. 7what does map() do?applies a function to every element in a sequence, producing a new sequence
  8. 8how do you use map to find ord of every character in 'the fox'x=list(map(ord, 'the fox'))
  9. 9what does map(ord, 'the fox') return?
  10. 10why does map return ?it uses lazy evaluation
  11. 11how do you solve lazy evaluation of map easily?deconstruct it immediately:
    list(map(ord, 'the fox'))
  12. 12how many input sequences can map() accept?any number, as long as number of inputs match number of function arguments
  13. 13what does map architcture look like for 3 seqences sent into a function that takes 3 input parameterswhat does map architcture look like for 3 seqences sent into a function that takes 3 input parameters
  14. 14sizes=['s','m','l'] colors=['blue','red','yel'] combine(size,color) map the function above using map()list(map(combine, sizes, colors))
  15. 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']
  16. 16if input sequences are not same size, when does map stop?when shortest sequence is exhausted
  17. 17what gives a new seqence of all true return values for a function for each element in a sequencefilter()
  18. 18given a function is_odd(num) x=[1,2,3,4,5,6] how would you filter x?filter (is_odd, x)
  19. 19x=[1,2,3,-4,-5,6] create a filter of all positive values of xpositives = filter(lambda ps: ps0, x)
  20. 20is filter lazy ?yes, has to be iterated
  21. 21how do you remove all elements in which evaluate to false (using filter)pass None as first argument to filter
  22. 22what is f in: trues = filter(None, [0,1,False,True]) f=list(trues)[1, True]
  23. 23how do you repeatedly reply a function to elements and reduce them to a valuefunctools.reduce()
  24. 24how would you reduce() f=[1,2,3,4,5] to sum them upfrom functools import reduce
    import operator
    reduce(operator.add, f)
  25. 25how do you set initial value of reduce()?set optional initial value, ie:
    values=[1,2,3]
    reduce(operator.add, values, 0)
  26. 26what should the initial value for reduce be when using operator.mul ?1
  27. 27how do you create an iterator?iter()
  28. 28how do you get next element in sequence?next()
  29. 29what signals the end of the iteration sequence?StopIteration
  30. 30what is an iterable ?an object that implements the __iter__() method
  31. 31what is an iterator?an object that implements the iterable protocol
    and implements the __next__() method
  32. 32when creating the code for __next__() method, how do you signal end of data?raise StopIteration()
  33. 33whats an alternative way to implement iterable protocol, supporting consecutive integer indexing?code __getitem__()
  34. 34what would the __get_item() return?def __getitem__(self, idx):
    return self.data[idx]
  35. 35when using iter(callable, sentinal) when would loop iter stop?when callable produces value equal to sentinal

Informações

Descrição
python (beyond basics python flashcards) FlashCards sobre python iterables and iteration, criado por Andrew Sperazza em 11-01-2017.
Criado por
Andrew Sperazza
Visualizações
2
Avaliação

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?