(anonymous functions) from Lisp & functional programming
tmp = lambda x: x**2
print(type(tmp))
tmp(2)
# forget about creating a new function name...just do it!
(lambda x,y: x**2+y)(2,4.5)
## create a list of lambda functions
lamfun = [lambda x: x**2, lambda x: x**3, \
lambda y: math.sqrt(y) if y >= 0 else "Really? I mean really? %f" % y]
for l in lamfun: print(l(-1.3))
# %load airline.py
airports = {"DCA": "Washington, D.C.", "IAD": "Dulles", "LHR": "London-Heathrow", \
"SVO": "Moscow", "CDA": "Chicago-Midway", "SBA": "Santa Barbara", "LAX": "Los Angeles",\
"JFK": "New York City", "MIA": "Miami", "AUM": "Austin, Minnesota"}
# airline, number, heading to, gate, time (decimal hours)
flights = [("Southwest",145,"DCA",1,6.00),("United",31,"IAD",1,7.1),("United",302,"LHR",5,6.5),\
("Aeroflot",34,"SVO",5,9.00),("Southwest",146,"CDA",1,9.60), ("United",46,"LAX",5,6.5),\
("Southwest",23,"SBA",6,12.5),("United",2,"LAX",10,12.5),("Southwest",59,"LAX",11,14.5),\
("American", 1,"JFK",12,11.3),("USAirways", 8,"MIA",20,13.1),("United",2032,"MIA",21,15.1),\
("SpamAir",1,"AUM",42,14.4)]
help(list.sort)
flights.sort(key=lambda x: x[4]) ;
print(flights)
import pprint
pprint.pprint(flights)
operator.itemgetter(item[, args...])¶
Return a callable object that fetches item from its operand using the operand’s __getitem__()
method. If multiple items are specified, returns a tuple of lookup values.
http://docs.python.org/library/operator.html#module-operator
import operator
flights.sort(key=operator.itemgetter(4,1,0))
flights
filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true
mylist=[num for num in range(101) if (num & 2) and (num & 1) and (num % 11 != 0.0)]
print(mylist)
def f(num): return (num & 2) and (num & 1) and (num % 11 != 0.0)
mylist = list(filter(f,range(101)))
print(mylist)
mylist = list(map(lambda num: (num & 2) and (num & 1) and (num % 11 != 0.0),range(101)))
print(mylist)
if the input is a string, so is the output...
## also works on strings...try it with lambdas!
import string
a="Charlie Brown said \"!@!@$@!\""
"".join([c for c in a if c in string.ascii_letters])
", ".join([str(num) for num in range(101) if (num & 2) and \
(num & 1) and (num % 11 != 0.0)])
[back]
def timesthree(x): return x*3
list(map(timesthree,"spam"))
list(map(lambda x: x**3, range(1,10)))
reduce(function, sequence) returns a single value constructed by calling the binary function on the first two items of the sequence, then on the result and the next item, and so on
from functools import reduce
# sum from 1 to 10
reduce(lambda x,y: x + y, range(1,11))
%timeit reduce(lambda x,y: x + y, range(1,11))
reduce(lambda x,y: x + y, range(11,1,-1))
list(range(11,1,-1))
a = ["a","b"]
reduce?
# sum() is a built in function...it’s bound to be faster
%timeit sum(range(1,11))
zip()
¶built in function to pairwise concatenate items in iterables into a list of tuples
list(zip(["I","you","them"],["=spam","=eggs","=dark knights"]))
list(zip(["I","you","them"],["=spam","=eggs","=dark knights"],["!","?","#"]))
a = list(zip(["I","you","them"],["=spam","=eggs","=dark knights"],["!","?","hello","blah"]))
a[0][1] = "=eggs"
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for a,q in zip(questions, answers):
print('What is your %s? It is %s' % (q,a))
not to be confused with zipfile
module which exposes file compression
http://snltranscripts.jt.org/91/91rstandup.phtml
http://www.nbc.com/saturday-night-live/video/standup-and-win/2868123
instead of ...
tmp = input("Enter a number and I'll square it: ") ; print(float(tmp)**2)
do this...
def f():
try:
tmp = input("Enter a number and I'll square it: ")
print(float(tmp)**2)
except:
print("dude. I asked you for a number and %s is not a number." % tmp)
finally:
print("thanks for playing!")
f()
try:
# volatile stuff here
tmp = raw_input("Enter a number “ + \
and I'll square it: ")
print(float(tmp)**2)
except:
# upon error, jump here inside except and execute that code
print("dude. I asked you for a number and " + \
"%s is not a number." % tmp)
finally:
# regardless of whether you hit an error, execute everything inside the finally block
print("thanks for playing!")
try:
print("eat at" % joes)
finally:
print("bye.")
exec
is a statement which executes strings as if they were Python code
a = "print('checkit')"
print(a)
exec(a)
a = "x = 4.56"
exec(a)
print(x)
exec("del x")
print(x)
import math
while True:
bi = input("what built in function would you like me to coopt? ")
if bi in ('?',"",'end'):
break
nn = input("what new name would you like to give it? ")
exec("%s = %s" % (nn,bi))
jsin(math.pi/2)
eval
is an expression which evaluates strings as Python expressions
x = eval('5') ; print(x) # x <- 5
x = eval('%d + 6' % x) ; print(x) # x <- 11
x = eval('abs(%d)' % -100) ; print(x) # x <- 100
eval('if 1: x = 4') # INVALID; if is a statement, not an expression.
Write a code which generates python code that approximates the function: $$x^2 + x$$
hints:
randomly generate lambda
functions using a restricted vocabulary:
voc =["x","x"," ","+","-","*","/","1","2","3"]
evaluate these lambda
functions at a fix number of x values and save the difference between those answers and x**2 + x
catch errors!
start with a file like:
import random
import numpy
voc =["x","x"," ","+","-","*","/","1","2","3"]
nfunc = 1000000
maxchars = 10 # max how many characters to gen
eval_places = numpy.arange(-3,3,0.4)
sin_val = eval_places**2 + eval_places
tries = []
for loop...