Python- Lambda, Map, Filter, Reduce Functions

Source: Deep Learning on Medium


Go to the profile of Develop It

#Lambda
#Map
#Filter
#Reduce
#These are new functions in Python 3 and Java 8.
#These functions are borrowed from functional programming languages like Lisp.

# — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

#Lambda Function:
#[
 #A simple 1-line function.
 #def or return keywords are not used as they are implicit.
 #The whole point of this function is to make it very short, simple, quick and easy to implement.

#Eg:
#Generally
def double(x):
 return x * 2

lambda x: 2 * x #x is Parameter, Returns 2 * x.

#Generally
def add(x, y):
 return x + y

lambda x, y: x + y #After : values are returned.

#Generally
def max(x, y):
 if x > y:
 return x
 else:
 return y
print(max(9, 1))

max = lambda x, y: x if x > y else y
print(max(9, 1))

#]

# — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

#Map Function:
#[
#Apply same function to each element of a sequence.
#Return the modified list.

#Generally
def square(list1):
 list2 = []
 for i in list1:
 list2.append(i**2)
 return list2

print(square([1, 2, 3, 4]))

list1 = [1, 2, 3, 4]
print(list(map(lambda x: x**2, list1)))
#map(function, list)

#print(list(map(square, list1)))

print([i**2 for i in list1]) #List Comprehension

#]

# — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

#Filter Function:
#[
#Filter items out of a sequence.
#Return filtered list.

def greater_two(list1):
 list2 = [i for i in list1 if i > 2]
 return list2

print(greater_two([1, 2, 3, 4]))

list1 = [1, 2, 3, 4]
print(list(filter(lambda x: x > 2, list1)))
#filter(condition, list)
#]

# — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

#Reduce Function:
#[
#Applies same operation to items of a sequence.
#Uses result of operation as first param of next operation.
#Returns an item, not a list.

def mult(list1):
 prod = list1[0]
 for i in range(1, len(list1)):
 prod *= list1[i]
 return prod

print(mult([1, 2, 3, 4]))

list1 = [1, 2, 3, 4]
#print(reduce(lambda x, y: x*y, list1))
#reduce(function, list)
#1 * 2 = 2
#2 * 3 = 6
#6 * 4 = 24
#]

# — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

#ASCII has 128 characters in it and it can be represented with 8 bits = 1 Byte.
#Unicode has billions of characters because it contains all the language characters and it can be represented with 32 bits = 4 Bytes.
#Unicode is represented in utf(Unicode Transformation Format) format.
 #UTF 8 is dynamic (8–32 bits (1–4 Bytes) it can be same as ASCII) (Compressed)
 #Usually we use UTF 8 to encode and decode data.
 #So when you’re dealing with old data then you’ve to convert the Byte into Unicode format which is UTF-8.
 #UTF 16 (16 bits) (Compressed)
 #UTF 32 (32 bits original one)
#In Python 2 a string str is a Byte.
#In Python 3 a string str is a Unicode.
#String Unicode -> encode() -> Bytes UTF-8 -> send() -> Socket -> Network
 # String Unicode <- decode() <- Bytes UTF-8 <- recv() <- 
#In Python or Pandas if you want to read a file then use encoding = ‘latin-1’ in the pd.read_csv().
#This will convert the other format into unicode format.
# — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -