Functional tools in Python : lambda, map, filter, reduce.

One of the powerful features of Python is the use of functional constructs, namely:

  • lambda
  • map
  • filter
  • reduce

 

lambda:

The general syntax of a lambda function is quite simple:
lambda argument_list: expression
The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.
The following example of a lambda function returns the sum of its two arguments:

>> f = lambda x, y : x + y

>> f(1,1)

 

map:

map() is a function with two arguments:

r = map(func, seq)

The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq. It returns a new list with the elements changed by func.

Tips:

[1] Using map with lambda

>> Celsius = [39.2, 36.5, 37.3, 37.8]

>> Fahrenheit = map(lambda x: (float(9)/5)*x + 32, Celsius)

>> print Fahrenheit

[102.56, 97.7, 99.14, 100.03999999999999]

 

[2]  Pandas has a ‘map’ helper function which is quite interesting

>> df[‘Gender’] = df[‘Sex’].map( lambda x: x[0].upper() )

>> df[‘Gender’] = df[‘Sex’].map({‘female’ : 0, ‘male’ : 1}).astype(int)

 

filter

The function filter(function, list) offers an elegant way to filter out all the elements of a list, for which the function function returns True.
The function filter(f,l) needs a function f as its first argument. f returns a Boolean value, i.e. either True or False. This function will be applied to every element of the list l. Only if f returns True will the element of the list be included in the result list.

>> fib = [0,1,1,2,3,5,8,13,21,34,55]

>> result = filter(lambda x: x % 2, fib)

>> print result [1, 1, 3, 5, 13, 21, 55]

>> result = filter(lambda x: x % 2 == 0, fib)

>> print result [0, 2, 8, 34]

 

reduce

>> reduce(lambda x,y: x+y, [47,11,42,13])

113

>> f = lambda a,b: a if (a > b) else b >>> reduce(f, [47,11,42,102,13])

102


 

 

Cut Command (Linux)

cut [-b] [-c] [-f list] [-n] [-d delim] [-s] [file]

Tips:

[1]  –d and -f

Option -d specified a single character delimiter (in the example above it is a colon) which serves as field separator. Option -f which specifies range of fields included in the output (here fields range from five till the end). Option -d presupposes usage of option -f.

[2]

Extraction of line segments can typically be done by bytes (-b), characters (-c), or fields (-f) separated by a delimiter (-d — the tab character by default). A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1

[3]

In case of CSV files, it might be tricky to use the comma (,) as delimiter.  CSV files often contain columns where there is comma embedded within quotes.

[4]  For TAB delimited files, there are 2 options to spedify the tab with the -d flag.

(a) Press Ctrl-v + Tab

cut -f2 -d'   ' infile

(b) or write it like this:

cut -f2 -d$'\t' infile

 

Examples:

[1]

$ head -n 20 ../../datasets/MLdatasets/titanic_train.csv | cut -d “,” -f 2,3