-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectionType.py
240 lines (193 loc) · 6.23 KB
/
collectionType.py
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'''
single value: primitive: int, float, str, bool
data collection in single variable: array
data + behavior/method: object
'''
"""
Collection/Iterables:
1. Mutable
2. Data positioning/ order
3. Data retrieve: indexing for ordered one
"""
# # List : ordered mutable collection of data
# lstStudent = ["name1", "name2"]
# lstTeacher = ["name1", 12, lstStudent, 1.0, 12, True]
# print(len(lstTeacher))
# # for ordered
# print(lstTeacher[1]) # left to right indexing
# print(lstTeacher[-1]) # right to left
# # mutable
# lstTeacher.append("Nabin")
# lstTeacher.extend([1, 2])
# print(lstTeacher)
# print(lstTeacher[-1]) # right to left
# lstTeacher.remove(12)
# print(lstTeacher)
# data1 = lstTeacher.pop()
# print(lstTeacher, data1)
# # spread operator *
# l1 = [1, 3]
# l2 = [2, 4]
# l3 = l1 + l2
# l4 = [l1, l2]
# l5 = [*l1, *l2]
# print(l3, l4, l5)
# tuple: ordered but immutable
# tuple is written with () but if no () used atleast , should be used!
data1 = (1, 2, 3)
# x = list(data1)
# x.append(12)
# print(x)
# # swap two number
# (x,y) = (1,2)
# print(x, y)
# x,y = y,x
# print(x, y)
# (a, b) = (1, 2) # multi variable assign using tuple property
# print(b*2)
# print(type(a))
# # dictionary unordered but indexing through key and is mutable
# # dict1 = {key: value} key: unique and immutable type value: can be of any data type
# studentDetails = {'sam': (12, 23, 12), 'hari': (
# 32, 23, 12), 'extra': "missing"}
# print(studentDetails['hari']) # reading the value of key
# print("The marks in Nepali is", studentDetails['hari'][2])
# # returns value of key if not found returns the default provided
# print(studentDetails.get("har", 0))
# # returns value of key if not found returns the default provided
# print(studentDetails.get("hari", 0))
# check = studentDetails.get("har", None)
# if check:
# print("the avg marks is ", sum(check)/len(check))
# else:
# print("No key found!")
# # If key exists when indexing the value is updated, if new key, key:value is added to dict
# studentDetails['sam'] = 12, 24, 14 # replacing with new value/ mutable
# print(studentDetails)
# studentDetails['age'] = 24 # adding new key value pair
# print(studentDetails)
# student = {
# 'name': "sandip",
# 'details': [1,2,3,5]
# }
"""
Student: Table rows and columns
Attributes: age, phone, name are the columns
Each record/ tuple: represented by row
1,2,3
4,2,1
5,2,1
"""
# matrix = [[1,2,3],[4,2,1],[5,2,1]] # matrix is represented by 2D list==> list having list of rows
# print(matrix[0][2]) # indexing first row is accessed followed by columns list[row][column]
# Slicing similar to range syntax list[start_index:stop_index:step]
# Start: start position and is inclusive
# Stop: exclusive ending index/position
# Step: jump value
# +ve: scanning left to right
# -ve: scanning right to left
numbers = list(range(1, 16))
print("number>>", numbers)
# enumerate(iterable, start=0)
# enumerate returns tuple from collection (index, element from collection)
numbers2 = tuple(enumerate(numbers))
# print(numbers2)
# step -ve: scan from right to left, start: -1(last) stop: 2 exclusive, upto 3
# x1 = numbers[-1:2:-2]
# print(numbers.index(4))
# print(x1)
# if start or stop value is empty, it takes min/ max start-stop value possible // what ever
x1 = numbers[-1::-1]
x1 = numbers[:-1]
x2 = numbers[-1]
print("X1>>", x1)
print("X2>>", x2)
# # matrix is represented by 2D list==> list having list of rows
# matrix = [[1, 2, 3], [4, 2, 1], [5, 2, 1]]
# # indexing first row is accessed followed by columns list[row][column]
# # print(matrix[0][2])
# print(matrix[:2])
# sliced = []
# for each in matrix[:2]:
# # print(each[1:])
# sliced.append(each[1:])
# print(sliced)
# Comprehension List and dictionary
lst = list(range(6))
print(lst)
sqrEven = [] # what else??
for data in lst:
if data % 2 == 0:
sqrEven.append(data**2)
elif data % 3 == 0:
sqrEven.append(data*2)
else:
sqrEven.append(data)
print(sqrEven)
# sqrEvenC = [data**2 for data in lst if data%2 == 0]
# sqrEvenC = [data**2 if data % 2 == 0 else data for data in lst]
# sqrEvenC = [data**2 if data % 2 == 0 else data *2 if data % 3 == 0 else data for data in lst]
# print(sqrEvenC)
student = ['sandip', 'binaya', 'prajwol']
nepali = [99, 100, 90]
# dic_student = {} # any other way??
# for each in student:
# # updating the dict with new items from the list givem
# dic_student[each] = len(each)
# dic_student = {var: len(var) for var in student}
# print(dic_student)
# dic_marks = {} # any other way??
# for i in range(len(student)):
# # updating the dict with new items from the list givem
# dic_marks[student[i]] = nepali[i]
# dic_marks = {student[i]: nepali[i] for i in range(len(student))}
# zip(iterable1,iterable2,iterable3.....) output: (it1.1,it2.1,it3.1), (it1.2,it2.2,it3.2)
# updated_coll = tuple(zip(student, nepali))
# print(tuple(updated_coll))
# dict_marks = {name: marks for name, marks in updated_coll}
# print(dict_marks)
# map, filter, lambda
# map/filter(fun, iterable) map===> each element of iterable is passed to function and changes to returned value
# filter >> returns elements of iterable if function condition is fulfilled, i.e fun should return true or false
def sqr(n):
return n**2
def add(a, b):
return a+b
def is_even(n):
if n % 2 == 0:
return True
else:
return False
x1 = map(sqr, filter(is_even, lst))
print(tuple(x1))
# lambda is anonymous function
x2 = map(lambda x: x+2, lst)
print(list(x2))
x2 = map(lambda x, y: x+y, lst, lst)
print(list(x2))
x3 = map(add, lst, lst)
print(list(x3))
# file = open('./fileStatus.txt') # relative path os module, wd +join
# # content = file.read()
# content = file.readlines()
# file.close()
# print(content)
database101 = []
with open('./fileStatus.txt') as file:
content = file.readlines()
# print(content[1].strip())
# print('test')
for each in content:
updated = each.strip().split('-')
database101.append(updated)
# # print(database101)
# # print(database101[1][2])
# print(database101[1:])
# sum_ = 0
# for each in database101[1:]:
# sum_ += int(each[1])
# avg = sum_//len(database101[1:])
# print(avg)
with open('sample.txt', 'w') as file:
file.write('sandip\n')
file.write(str(101))