#!/usr/bin/env python
# coding: utf-8
# # List Comprehensions & Higher order functions
# - list is a very powerful and commonly used container
# - list shortcuts can make you an efficient programmer
# - Ref: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
# - E.g., in maths: S = {x2 : x in {0 ... 9}}
# In[1]:
# Typical way to create a list of squared values of list 0 to 9?
sq = []
for i in range(10):
sq.append(i**2)
# In[2]:
print(sq)
# In[3]:
# List comprehension -- handy technique:
S = [x**2 for x in range(10)]
# In[4]:
S
# In maths: V = (1, 2, 4, 8, ... 2 12)
# In[5]:
# In python ?:
V = [2**x for x in range(13)]
print(V)
# In maths: M = {x | x in S and x even}
# In[17]:
# In python:
M = [x for x in S if x%2==0]
# In[18]:
M
# In[12]:
#sentence = "The quick brown fox jumps over the lazy dog"
#words = sentence.split()
# can make a list of tuples or list of lists
wlist = [(w.upper(), w.lower(), len(w)) for w in "The quick brown fox jumps over the lazy dog".split()]
# In[13]:
wlist
# # Lambda Functions
# - anonymous functions (no name)
# - typically used in conjunction with higher order functions such as: map(), reduce(), filter()
# - Reference: http://www.secnetix.de/olli/Python/lambda_functions.hawk
# ## Difference between lambda and normal function
# In[2]:
def func(x): return x**2
# In[3]:
print(func(4))
# In[4]:
g = lambda x: x**2 # no name, no parenthesis, and no return keyword
# In[5]:
print(g(4))
# ### lambda function properties and usage
# - single line simple functions
# - no explicit return keyword is used
# - always contains an expression that is implictly returned
# - can use a lambda definition anywere a function is expected without assigning to a variable
# - syntax: ** lambda argument(s): expression **
# ### higher order functions and lambda application
# - map, reduce, filter, sorted built-in functions
# ### sorted()
# In[6]:
list1 = ['Apple', 'apple', 'ball', 'Ball', 'cat']
list2 = sorted(list1, key=lambda x: x.lower())
# In[7]:
print(list2)
# In[8]:
list3 = [('cat', 10), ('ball', 20), ('apple', 3)]
from operator import itemgetter
list5 = sorted(list3, key=itemgetter(1), reverse=True)
# In[9]:
print(list5)
# In[10]:
list6 = sorted(list3, key=lambda x: x[1], reverse=True)
# In[11]:
print(list6)
# ### filter()
# - filter elemets in the list by returning a new list for each element the function returns True
# In[13]:
help(filter)
# In[14]:
list7 = [2, 18, 9, 22, 17, 24, 8, 12, 27]
list8 = list(filter(lambda x: x%3==0, list7))
# In[15]:
print(list8)
# ### map()
# In[16]:
help(map)
# In[17]:
items = list(range(1, 11))
squared = list(map(lambda x: x**2, items))
# In[18]:
print(squared)
# In[19]:
# map each words with its length
words = "The quick fox jumps over the lazy dog".split()
words = [word.lower() for word in words]
# In[20]:
print(words)
# In[21]:
w_len = list(map(lambda w: (w, w.upper(), len(w)), words))
# In[22]:
print(w_len)
# ### reduce()
# - reduce() is found in functools module
# - used to reduce a list of values to a single output
# In[23]:
import functools
help(functools)
# ### find sum of first n values
# In[24]:
s = functools.reduce(lambda x,y:x+y, range(1, 11))
# In[25]:
assert sum(range(1, 11)) == s
# ### find factorial (or product of) first n values
# In[26]:
fact = functools.reduce(lambda x,y:x*y, range(1, 11))
# In[27]:
fact
# In[28]:
import math
assert math.factorial(10) == fact
# In[ ]: