-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.List_functions.py
More file actions
195 lines (142 loc) · 6.78 KB
/
3.List_functions.py
File metadata and controls
195 lines (142 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
print("------------------------------------------------------------------------------------------")
print("---------------------------------Lists Function-------------------------------------------")
print("------------------------------------------------------------------------------------------")
n=[10,20,30,40]
print("---------------------------------Len Function------------------------------------------")
# len(): return the number of elements present in the list.
# syntax: len(list)
print("Length of the list: ",len(n))
print("---------------------------------Sum Function------------------------------------------")
# sum(): sum up the numbers in the list.
# syntax: sum(iterable numbers)
print("Sum of the list",sum(n))
Sum = sum(n, 10)
print(Sum)
print("-----------------------------------Split Function------------------------------------------")
# split(): split the strings and store it to a list
# syntax: list = string.split()
lst=[]
# input the list as string
string = "Harrish P T"# input("Enter elements (Space-Separated): ")
# split the strings and store it to a list
lst = string.split()
print('Using Split function to add string in a list is:', lst) # append a string in a list is: ['Harrish', 'P', 'T']
print("Using Map : ")
# # input size of the list
# n = 3 # int(input("Enter the size of list : "))
# store integers in a list using map,
# split and strip functions
# lst = list(map(int, input("Enter the integer elements: ").strip().split()))[:n]
# printing the list
# print('Using Split function get the element in a list: ', lst)
print("---------------------------------Reversed Function----------------------------------------")
# reversed(): The reversed() function returns a reverse iterator,
# which can be converted to a list using the list() function.
n=[1, 2, 3, 4, 5]
reversed_list=list(reversed(n)) # without list function it return reversed object
print("Reversed element ",reversed_list) # [5, 4, 3, 2, 1]
# ord(): Returns an integer representing the Unicode code point of the given Unicode character.
# cmp(): This function returns 1 if the first list is “greater” than the second list
print("------------------------------------Min Function------------------------------------------")
# min(): return minimum element of a given list
# syntax: min(a, b, c, …, key=func)
# Parameters
# a, b, c, .. : similar type of data.
# key (optional): A function to customize the sort order
# Example 1:
numbers = [23,25,65,21,98]
print("Print the minimum number in the list: ",min(numbers))
print("Print the minimum number in two numbers: ",min(15,75))
# Example 2:
square = {25: 10, 8: 64, 2: 4, 3: 9, -1: 1, -2: 4}
print("Print the minimum number in dict key:", min(square)) # -2
key2 = min(square, key = lambda k: square[k]) # key parameter[-6,8,2,3,-1,-2]
print("Print the minimum number in dict value:", square[key2]) # 1
# Example 3:
languages = ["C","Python", "C++ Programming", "Java", "JavaScript",'PHP','Kotlin']
print("Print the minimum string in ascending order: ", min(languages))
# Example 4:
def func(x):
return abs(x - 10)
print(min([6, 12, 13, -4, 5], key=func))
print("------------------------------------Max Function------------------------------------------")
# max(): return maximum element of a given list
print("---------------------------------Reduce Function------------------------------------------")
# reduce():
# The reduce(fun,seq) function is used to apply a particular function passed in its argument to all
# of the list elements mentioned in the sequence passed along.
# This function is defined in “functools” module.
from functools import reduce
import operator
print("Using Lambda : ")
lis = [1, 3, 5, 6, 2]
print("The sum of the list elements is : ", end="")
print(reduce(lambda a,b: a+b, lis))
print("The maximum element of the list is : ", end="")
print(reduce(lambda a, b: a if a > b else b, lis))
print("-------------------------------------------------------------------")
# Using Operator Functions
# reduce() can also be combined with operator functions to achieve the similar functionality as with
# lambda functions and makes the code more readable.
# initializing list
print("Using Operator : ")
# using reduce to compute sum of list
# using operator functions
print("The sum of the list elements is : ", end="")
print(reduce(operator.add, lis))
# using reduce to compute product
# using operator functions
print("The product of list elements is : ", end="")
print(reduce(operator.mul, lis))
# using reduce to concatenate string
print("The concatenated product is : ", end="")
print(reduce(operator.add, ["Hello ", "World, ", "Harrish"]))
print("-------------------------------------------------------------------")
print("Using accumalate : ")
# reduce() vs accumulate()
# Both reduce() and accumulate() can be used to calculate the summation of a sequence elements.
# But there are differences in the implementation aspects in both of these.
# reduce() is defined in “functools” module,
# accumulate() in “itertools” module.
# reduce() stores the intermediate result and only returns the final summation value.
# Whereas, accumulate() returns a iterator containing the intermediate results.
# The last number of the iterator returned is summation value of the list.
# reduce(fun, seq) takes function as 1st and sequence as 2nd argument.
# In contrast accumulate(seq, fun) takes sequence as 1st argument and function as 2nd argument.
# Syntax
# itertools.accumulate(iterable[, func]) –> accumulate object
from itertools import accumulate
iterable1 = [1, 2, 3, 4, 5]
# using the itertools.accumulate()
result_object = accumulate(iterable1,operator.mul)
# printing each item from list
print("Multiply Iterable in list using accumate: ")
for each in result_object:
print(each)
# Explanation :
# The operator.mul takes two numbers and multiplies them.
# operator.mul(1, 2)
# 2
# operator.mul(2, 3)
# 6
# operator.mul(6, 4)
# 24
# operator.mul(24, 5)
# 120
iterable2 = [5, 3, 6, 2, 1, 9, 1]
# using the itertools.accumulate()
# Now here no need to import operator
# as we are not using any operator
# Try after removing it gives same result
result = accumulate(iterable2, max)
# printing each item from list
for each in result:
print(each)
# printing summation using accumulate()
print("The summation of list using accumulate is :", end="")
print(list(accumulate(lis, lambda x, y: x+y)))
# printing summation using reduce()
print("The summation of list using reduce is :", end="")
print(reduce(lambda x, y: x+y, lis))
print("---------------------------------Ord Function------------------------------------------")
# Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument.