pickle
return
values to communicate their resultsfile.write(.)
file.writelines(.)
from os import linesep # ... long computation result = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] with open('result.txt', 'w', encoding='utf-8') as outfile: outfile.write("These numbers solve the world's problems!" + linesep) for row in result: for elem in row: outfile.write(str(elem) + ' ') outfile.write(linesep)
from os import linesep # ... long computation result = [[1, 3, 2], [4, 5, 6], [9, 8, 7]] with open('result.txt', 'a', encoding='utf-8') as outfile: # append outfile.write("Now we really solve the world's problems!" + linesep) for row in result: for elem in row: outfile.write(str(elem) + ' ') outfile.write(linesep)
pickle
# `result` needs to be defined before writing it import pickle with open('test.dump', 'bw') as f: pickle.dump(result, f)
import pickle with open('test.dump', 'br') as f: result = pickle.load(f)
lambda
function
lambda arg [, arg2, ...]: expression
f = lambda x, y: x + y result = f(3, 4)
m = [[2, 'student', 'master'], [3, 'student', 'bachelor'], [1, 'works', 'full time']] m.sort()
m = [[2, 'student', 'master'], [3, 'student', 'bachelor'], [1, 'works', 'full time']] m.sort(key=lambda x: x[2])
#!/usr/bin/env python3 names = ['betty', 'Anna', 'Sue', 'gernot', 'Luis', 'Therese', 'amelie', 'Marion'] names.sort() # likely not what we'd expect names.sort(key=lambda x: x.lower())
#!/usr/bin/env python3 def factorial(n): """ Return the factorial of n. """ assert n >= 0 if n <= 1: return 1 return n * factorial(n - 1) print(factorial(5))
def fib(n): """ Return the n-th fibonacci number (starting at 1). Note: this is an inefficient implementation. """ if n <= 1: return n return fib(n - 1) + fib(n - 2) print(fib(5))
map(function, *iterables)
- compute the function using arguments from
the iterablesmap(.)
returns an iterator#!/usr/bin/env python3 squares = map(lambda x: x ** 2, [1, 2, 3, 4, 5]) print(list(squares))
filter(function, iterable)
- yield items of iterable for which function(item) is true#!/usr/bin/env python3 odd_numbers = filter(lambda x: x % 2, [1, 2, 3, 4, 5]) print(list(odd_numbers))
reduce(function, iterable[, initial])
- apply a function of two arguments cumulatively to the items of sequence#!/usr/bin/env python3 import functools product = functools.reduce(lambda x, y: x * y, [1, 2, 3, 4, 5]) print(product)
operator
module provides functions for Python's operators
operator.mul
corresponds to lambda x, y: x * y
all(iterable) -> bool
map
with a predicate (a boolean valued function) to create the inputall(map(lambda x: x % 2, [1, 2, 3])) # False all(map(lambda x: x % 2, [1, 3, 17])) # True
any(iterable) -> bool
#!/usr/bin/env python3 squares = [x ** 2 for x in [1, 2, 3, 4, 5]] print(squares) odd_numbers = [x for x in [1, 2, 3, 4, 5] if x % 2] print(odd_numbers)
#!/usr/bin/env python3 """ Simple dict comprehension exchanging the keys and values of a dict. Assumes that the values are unique and hashable. """ d = {'Martin': 7, 'Sue': 5, 'Pat': 12, 'Chris': 4} print(d) d = {v: k for k, v in d.items()} print(d)
#!/usr/bin/env python3 squares = (x ** 2 for x in [1, 2, 3, 4, 5]) print(squares) print(list(squares))
functools.partial(.)
)