Data Warehousing and Data Science

2 December 2020

Python: Function, Lambda, Map, Filter, Reduce

Filed under: Python — Vincent Rainardi @ 6:43 am

A function with 2 arguments:

def f(a,b):
    c = a * b
    return c
print(f(2,3))
Output: 6

Recursive function:

def factorial(n):
    if n>1: return n*factorial(n-1)
    else: return n
print(factorial(3))
Output: 6

Star argument means I don’t know how many arguments. The argument is stored as a tuple.

def total(*args):
    return(sum(args))
print(total(1,2,3))
Output: 6
def proper_case(a):
    return " ".join([word[0].upper() + word[1:] for word in a.split()])
print(proper_case("going to town"))
Output: Going To Town

Lambda is a shortcut to create a function on the fly.

f = lambda a,b: a*b
print(f(2,3))
Output: 6

Use map to pair a function to a list (to run the function to every element in the list)
Use filter to apply a condition to a list, to look for elements which satisfy that condition.
Use reduce to apply a function to a pair of values each time, repeatedly (reduce is in functools library)

# Using map and lambda to find words beginning with a
L= ['Apple', 'Andy', 'Banana', 'Ben']
list(map(lambda x: 1 if x[0].lower() == 'a' else 0, L))
Output: [1, 1, 0, 0]
# Using map and lambda to produce cube numbers
input_list = [1,2,3]
list(map(lambda x: x**3, input_list))
Output: [1, 8, 27]
# Using map to pair 2 lists
def v_add(x,y): return(x+y)
list1 = [1,2,3] #Argument1 is a list
list2 = (4,5,6) #Argument2 is a tuple
print(list(map(v_add, list1, list2))) #We pass 2 arguments to map
Output: [5, 7, 9]
# Using map to pair 2 lists
L1 = ['P','O']
L2 = ['X','Y']
list(map(lambda x,y: x + ' ' + y, L1, L2))
Output: ['P X', 'O Y']
# Using filter to find the even numbers 
f = lambda x:  x%2 == 0
L = [1,2,3,4,5,6]
list(filter(f, L))
Output: [2, 4, 6]

# Now using lambda
list(filter(lambda x: x % 2 == 0, L))
Output: [2, 4, 6]
# Using filter to find the words that starts with a and ends with y
L = ['Apple', 'Andy', 'Banana', 'Ben']
list(filter(lambda x: x[0].lower()=='a' and x[-1].lower()=='y', L))
Output: ['Andy']
# Using reduce to sum the input
from functools import reduce
def v_add(x,y): return(x+y) 
reduce(v_add, range(1,4))
Output: 6

# Now using lambda
reduce(lambda x, y: x+y, range(1,4))
Output: 6
# Using reduce to find the largest number
L = [22,45,32,20,87,94,30]
def v_max(x,y):
    if x>y: return x
    else: return y
#or v_max = lambda x,y: x if x>y else y
reduce(v_max,L)
# Using reduce to concatenate letters
L = ['A','B','C']
v_concat = lambda x,y: x+y
reduce(v_concat, L)
Output: 'ABC'
# Using reduce to calculate 1 x 2 x 3 x 4 x ...
def f(x,y): return(x*y)
n = 4
L = list(range(1,n+1))
print(1 if n == 0 else reduce(f,L))
Output: 24

1 December 2020

Python: List comprehension

Filed under: Python — Vincent Rainardi @ 7:05 am

List comprehension means using for loop to create a list :

L = [i*2 for i in range(1,5)] #5 not included
print(L)
Output: [2,4,6,8]

We can use 2 for loops to get the words in a paragraph :

paragraph = ["This is sentence one." , 'This is sentence two.']
result  = [word for sentence in paragraph for word in sentence.split()]
print(result)
Output:
['This', 'is', 'sentence', 'one', 'This', 'is', 'sentence', 'two']

We can use 2 for loops and an if to get words beginning with certain letters :
(the example below is using the paragraph defined above)

letters = ['s','o']
result = [word for sentence in paragraph for word in sentence.split() if word[0].lower() in letters]
print(result)
Output:
['sentence', 'one.', 'sentence']

We can use an if with any conditions we like :

numbers = [100,200,300,400]
result = [n for n in numbers if n > 200]
print(result)
Output:
[300,400]

We can make a list using double for in i and j format like this:

product_list = [ i*j for i in range(1,3) for j in range(10,20,5) ] #3 and 20 are not included
print(product_list)
Output:
[10, 15, 20, 30]

We can make a dictionary too using for :

result = {i:i*3 for i in range(1,7,2)}
print(result)
Output:
{1: 3, 3: 9, 5: 15}

We can use for to access the key in a dictionary.
In this example I try to get the best Hogwart pupils in a dictionary (highest remarks):

Hogwarts = {1:['Harry',80] , 2:['Ron',70], 3:['Hermione',90], 4:['Neville',60], 5:['Seamus',50]}
best_pupils = {key:value[0] for key,value in students_data.items() if value[1] >= 70}
print(best_pupils)
Output:
{1: 'Harry', 2: 'Ron', 3: 'Hermione'}

Various examples of creating lists and dictionary using for :

#times 2 if i is divisible by 3, otherwise plus 2
result = [i*2 if i%3==0 else i+2 for i in range(1,7)]
print(result)
Output: 
[3, 4, 6, 6, 7, 12]

#create a dictionary containing cube numbers from 1 to 4
n = 4
result = {i:i**3 for i in range(1,n+1)}
print(result)
Output: 
{1: 1, 2: 8, 3: 27, 4: 64}

#A list containing combinations of letters from 2 words
result = [i+j for i in "po" for j in "he"]
print(result)
Output: 
['ph', 'pe', 'oh', 'oe']

#Create a dictionary from a word. The key is in upper case, the value is double letter.
result = {x.upper(): x*2 for x in 'potter'}
print(result)
Output: 
{'P': 'pp', 'O': 'oo', 'T': 'tt', 'E': 'ee', 'R': 'rr'}

Python: If and For

Filed under: Python — Vincent Rainardi @ 6:11 am

if-elif-else ends with : and the next line must be indented (can be on the same line though).
Equal is ==, non equal is !=.
Use and, or to combine conditions.

a = 1
if a == 0: print("A") 
elif a != 1: print("B")
elif 4 > a == 8: print("C")
else: print("D")
Output: D
a,b = 5,6
if a==5 and b==7: print("A") 
else: print("B")
if a==5 or b==6: print("A")
else: print("B")
Output:
B
A

for can be used with rangestringlisttupledictionaries or enumerate.
range(a,b,c) means from a to b step c but does not include b.

for i in range(1,5,2): print(i)
Output:
1
3
for i in reversed(range(1,5,2)): print(i)
Output:
3
1

in can be used with a list (without range) like this:

for i in "abc": print(i)
for i in [1,4,2]: print(i)
for i in (1,4,2): print(i)

in can be used with a dictionary like this:

for i,j in {"name":"Harry","age":11}.items(): print(i,j)
Output:
name Harry
age 11

D = {1:["Harry", 11], 2:["Ron", 11], 3:["Hermione", 10]}
for i,j in D.items(): print(i,j)
Output
1 ["Harry", 11]
2 ["Ron", 11]
3 ["Hermione", 10]

for(i,j) can use with enumerate to get the counter in i, like this:

for i,j in enumerate("abc"): print(j, i)
Output:
a 0
b 1
c 2

In for loop we can use break, continue, pass :

a = ''
b = 'abcdef'
for i in range(0,6):
  a += b[i]
  if i == 2: break #pass or continue
print(a)
Output: abc

Use zip to loop around 2 lists together :

list1,list2 = [1,2,3],[10,20,30]
for i,j in zip(list1,list2): print('{0}  {1}'.format(i,j))
Output:
1  10
2  20
3  30

We can use while to make a loop too :

i = 0
while i <= 3:
    print(i)
    i += 1
Output:
0
1
2
3

Use add to append to the range used in the for loop.
Note: add doesn’t output anything.

a = {1, 2}
for i in range(3,5): #5 is not included
    print(a.add(i)) #add doesn't output anything
print(a)
Output:
None
None
{1, 2, 3, 4}

Combining for, if, modulo and pass:

for i in range(1,4): #4 is not included
    if (i % 2 == 0): #modulo means "the reminder"
        print(str(i) + " is divisible by 2")
        pass #after printing 2, continue with 2 (stay in the loop)
    print(str(i) + " is not divisible by 2")
Output:
1 is not divisible by 2
2 is divisible by 2
2 is not divisible by 2
3 is not divisible by 2

Blog at WordPress.com.