In [10]:
# Praxis Business School
# Prithwis Mukerjee
#
# Data Structures in Python
# there are some changes that are necessary for these codes to work in Python 3
# range, filter, map return objects that are not lists … hence we need to cast them into lists
# reduce has been replaced with functools.reduce
# these changes are shown in red
#http://stackoverflow.com/questions/12319025/filters-in-python
#http://stackoverflow.com/questions/13638898/how-to-use-filter-map-and-reduce-in-python-3
print("Hello to Data Structues")
In [11]:
print("Filter Functions ................................")
pList1 = list(range(1,10))
print(pList1)
def pEven(x) : return x%2 == 0
pList2 = list(filter(pEven,pList1))
print(pList2)
pList3 = ["Ram", "RamPM", "Shyam", "PMShyam", "Sita", "SiPMMta"]
print(pList3)
def hasPM(x) : return "PM" in x
pList4 = list(filter(hasPM, pList3))
print(pList4)
In [12]:
# Lambda functions
print("Lambda Functions ................................")
pList2a = list(filter (lambda x : x%2 == 0, pList1))
print(pList2a)
pList4a = list(filter(lambda x: "PM" in x, pList3))
print(pList4a)
In [13]:
# Map
print("Map Functions ................................")
pList5 = list(map(lambda x : x*x, pList1)) # square each number in the list
print(pList5)
pList6 = list(map(lambda x : "Y"+x, pList3)) # appends the char Y to each string in the list
print(pList6)
pList7 = map(lambda x : len(x), pList3) # returns the number of characters in each string of the list
print(pList7)
pList8 = map(lambda x : (x, len(x)),pList3) # returns a list of tuples
print(pList8)
In [14]:
# Reduce
print("Reduce Functions ................................")
import functools
pVal1 = functools.reduce(lambda x,y: x+y, pList1) # adding the numbers in a list
print(pVal1)
pVal2 = functools.reduce(lambda x,y: x+y, pList3) # concatenating string in a list
print(pVal2)
pVal3 = functools.reduce(lambda x,y: (x[0]+y[0], x[1]+y[1]), pList8) # adding and concatenating elements in a list
print(pVal3)
In [ ]:
No comments:
Post a Comment