|
| 1 | +<h1 align="center"><a href="#"> Strings </a></h1> |
| 2 | + |
| 3 | +* The Python interpreter has a number of functions and types built into it that are always available. |
| 4 | +* There is no need to import modules extra in order to use them. |
| 5 | +* A few functions that we will be covering here are- |
| 6 | + * Map |
| 7 | + * Filter |
| 8 | + * Sort |
| 9 | + * Max/Min |
| 10 | + * Reduce |
| 11 | + |
| 12 | +## Map |
| 13 | +* It iterates through all the elements of a iterable and applies some specific funtion to each element of that iterable. |
| 14 | +* With multiple iterables, the iterator stops when the shortest iterable is exhausted |
| 15 | + |
| 16 | +### syntax |
| 17 | +```python |
| 18 | +r = map(func, seq) |
| 19 | +#takes each element in seq |
| 20 | +#applies func function to them |
| 21 | +#inserts them to r |
| 22 | + |
| 23 | +``` |
| 24 | +### implementation |
| 25 | +```python |
| 26 | + |
| 27 | +#for a single list involved |
| 28 | +a=['2011-12-22 46:31:11','2011-12-20 20:19:17', '2011-12-20 01:09:21'] |
| 29 | +dt=list(map(str.split, a)) #split function is applied to each element and it returns a list corresponding to each element |
| 30 | +print(dt) |
| 31 | + |
| 32 | +#for multiple lists involved |
| 33 | +a = [1,2,3,4] |
| 34 | +b = [17,12,11,10] |
| 35 | +print(list(map(lambda x,y:x+y, a,b))) |
| 36 | +#goes through each element of a,b |
| 37 | +#adds them correspondingly till one of the lists end |
| 38 | + |
| 39 | +``` |
| 40 | +Output |
| 41 | +``` |
| 42 | +[['2011-12-22', '46:31:11'], ['2011-12-20', '20:19:17'], ['2011-12-20', '01:09:21']] |
| 43 | +[18, 14, 14, 14] |
| 44 | +``` |
| 45 | +## Filter |
| 46 | +* As the name justifies,filter creates a list of elements for which a function returns true. |
| 47 | + |
| 48 | +### Syntax |
| 49 | +```python |
| 50 | +filter(filter_fnc,list) |
| 51 | +``` |
| 52 | +### implementation |
| 53 | +```python |
| 54 | +def fn(x): |
| 55 | + return (x<0) #(x<0) is condition automatically returns true or false |
| 56 | +number_list = range(-10, 10) |
| 57 | +less_than_zero = list(filter(fn, number_list)) |
| 58 | +print(less_than_zero) |
| 59 | +``` |
| 60 | +Output |
| 61 | +``` |
| 62 | +[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1] |
| 63 | +``` |
| 64 | + |
| 65 | +## Sort/Sorted |
| 66 | +* Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable |
| 67 | +* There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various manuals describing them |
| 68 | +* The key difference between both is that sorted returns a new list whereas sort chsnges the original list itself. |
| 69 | + |
| 70 | +### syntax |
| 71 | +```python |
| 72 | +list.sort() |
| 73 | +sorted(container)#sort in ascending order |
| 74 | +sorted(list,reverse=True)#sort in descending order |
| 75 | +sorted(list,key=<insert the key>)#sort in ascending order of the key set |
| 76 | +``` |
| 77 | +### implementation |
| 78 | +```python |
| 79 | + |
| 80 | +a = [5, 2, 3, 1, 4] |
| 81 | +a.sort()# .sort() function is specific to lists |
| 82 | +print(a) |
| 83 | + |
| 84 | +chir={2: 'D', 1: 'B', 4: 'B', 3: 'E', 5: 'A'}# declaring a dictionary |
| 85 | +for ind in sorted(chir): #sorted is general approach, although return type is a list |
| 86 | + print("{}:{}".format(ind,chir[ind]),end=" ") |
| 87 | +print() |
| 88 | +for ind in sorted(chir,reverse=True): #note the order is descending of the keys |
| 89 | + print("{}:{}".format(ind,chir[ind]),end=" ") |
| 90 | +def gv(key): |
| 91 | + return chir[key]; |
| 92 | +print() |
| 93 | +for ind in sorted(chir,key=gv,reverse=True): #note the order is descending of the values of each key |
| 94 | + print("{}:{}".format(ind,chir[ind]),end=" ") |
| 95 | + |
| 96 | +``` |
| 97 | +Output |
| 98 | +``` |
| 99 | +[1, 2, 3, 4, 5] |
| 100 | +1:B 2:D 3:E 4:B 5:A |
| 101 | +5:A 4:B 3:E 2:D 1:B |
| 102 | +3:E 2:D 1:B 4:B 5:A |
| 103 | +
|
| 104 | +``` |
| 105 | +## max/min |
| 106 | +* Return the greatets/smallest item in an iterable or the smallest of two or more arguments. |
| 107 | +* It can also be made to make use of key argument as used in sort function |
| 108 | +### implementation |
| 109 | +```python |
| 110 | +list = [1, 4, 3, 5,9,2] |
| 111 | +print(max(list)) #returns maximum value of list |
| 112 | +print(min(list)) #returns minimum value in the list |
| 113 | +print(max(list[2:-2])) #returns max value compined with slicing |
| 114 | +print(min(list[4:]) |
| 115 | +print(max(4,2,3,5)) |
| 116 | +print(min(7,2,1,8,4)) |
| 117 | +``` |
| 118 | +Output |
| 119 | +``` |
| 120 | +9 |
| 121 | +1 |
| 122 | +5 |
| 123 | +9 |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +## Reduce |
| 128 | +* Reduce is a really useful function for performing some computation on a list and returning the result. |
| 129 | +* It applies a rolling computation to sequential pairs of values in a list |
| 130 | +>note this not a inbuilt function as such because it needs to be imported from func tools but is pretty useful. |
| 131 | + |
| 132 | +### implementation |
| 133 | +```python |
| 134 | +#Normal way to find product of elements in a list |
| 135 | +product = 1 |
| 136 | +list = [1, 2, 3, 4] |
| 137 | +for num in list: |
| 138 | + product = product * num |
| 139 | + |
| 140 | +#Using reduce function to do so |
| 141 | +from functools import reduce |
| 142 | +def func(x,y): |
| 143 | + return x*y |
| 144 | +product = reduce(func, [1, 2, 3, 4]) |
| 145 | + |
| 146 | +``` |
| 147 | +Output |
| 148 | +``` |
| 149 | +24 |
| 150 | +24 |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | + |
0 commit comments