Write notes for list operations
>>> a = ["a", "b", "c"]
>>> b = [1, 2, 3]
>>> dict(zip(a,b))
{'a': 1, 'c': 3, 'b': 2}
>>> #Leave only 5 or more elements
>>> a = [3, 6, 4, 7, 2, 10]
>>> filter(lambda x: x >= 5, a)
[6, 7, 10]
>>>
>>> #Same as the bracket below
>>> fn = lambda x: x >= 5
>>> [i for i in a if fn(i)]
[6, 7, 10]
>>> #Add 5 to all numbers
>>> a = [3, 6, 4, 7, 2, 10]
>>> map(lambda x: x + 5, a)
[8, 11, 9, 12, 7, 15]
>>>
>>> #Same as the bracket below
>>> fn = lambda x: x + 5
>>> [fn(i) for i in a]
[8, 11, 9, 12, 7, 15]
>>> #Add all
>>> a = [3, 6, 4, 7, 2, 10]
>>> reduce(lambda x,y: x+y, a)
32
>>> #ascending order
>>> a = [3, 6, 4, 7, 2, 10]
>>> sorted(a)
[2, 3, 4, 6, 7, 10]
Recommended Posts