In [9]:
words = 'The quick brown fox jumps over the lazy dog'.split()
print words
stuff = []
for w in words:
stuff.append([w.upper(), w.lower(), len(w)])
for i in stuff:
print i
Use list comprehension and lambda/map function to define stuff.
In [10]:
stuff = map(lambda w: [w.upper(), w.lower(), len(w)],words)
for i in stuff: print i
Use the filter function to remove all the vowels from the sentence
In [11]:
sentence = "It's a myth that there are no words in English without vowels."
vowels = 'aeiou'
In [12]:
result = filter(lambda x: x not in vowels, sentence)
print result
Use the reduce function to find the produc of all the entries in the list [47,11,42,102,13]
In [13]:
print reduce(lambda x,y: x*y, [47,11,42,102,13])
In [15]:
# note that you can improve the speed of the calculation using built-in functions
# or better still: using the numpy module
from operator import mul
import numpy as np
a = range(1, 101)
print "\nreduce(lambda x, y: x * y, a)"
%timeit reduce(lambda x, y: x * y, a) # (1)
print "\nreduce(mul, a)"
%timeit reduce(mul, a) # (2)
print "\nnp.prod(a)"
a = np.array(a)
%timeit np.prod(a) # (3)
In [ ]: