-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList data structure.py
539 lines (408 loc) · 13 KB
/
List data structure.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
'''
----> List:
* It is a ordered mutable collection of elements.
* It is denoted by [].
* Insertion order is preserved.
* Hetrogenious objects are allowed.
* Duplicate values are allowed.
* Growable in nature.
* Indexing and slicing supported.
------------------------------------------------or---------------------------------------------------
* It is one datatype.
* It represented by "[]".
* It is a mutable datatype. "Mutable means changeable datatype."
* It stores different types of elements.
* It allows duplicates elements.
* It allows indexing.
Methods:
* append()
* extend()
* Remove()
* pop()
* count()
* index()
For slicing:
[start:stop:skip] ------> short name as s3
Ex:
a = [1 , 2.2 , "True" , "Python"]
---------> Positive indexing[0,1,2,3]
Negative indexing<-----------------[-1,-2,-3,-4]
1. CREATION OF LIST :
---------------------
a = []
print(type(a))
print(a)
2. ACCESSING THE ELEMENTS IN THE LIST:
---------------------------------------
a = [1,2,3,4,5]
print(a[0])
print(a[1])
print(a[-1])
print(a[100])
print(type(a))
3. DYNAMIC INPUT :
-------------------
a = eval(input("Enter the list: "))
print(a)
print(type(a))
4. RANGE WITH LIST :
--------------------
a = list(range(1,11))
print(a)
print(type(a))
5. SPLIT() FUNCTION :
---------------------
a = "Hello World"
b = a.split()
print(b)
print(type(b))
NOTE : Sometimes we can create list inside another list , such type of list called as nested list.
EXAMPLE : l=[10,20,30,40,[50,60]]
'''
'''
--------------------------------------------------------------------ACCESSING ELEMENTS OF LIST--------------------------------------------------------------
1.BY USING INDEX :
-------------------
* List follows zero based index i.e index of first element is zero
* list supports both positive and negative indexing.
'''
# Example :
# l = [10,20,30,40]
# print(l[0])
# print(l[1])
# print(l[-1])
# print(l[100])
'''
2. BY USING SLICE OPERATOR :
----------------------------
* We can access multiple elements of list by using slice operator.
* Syntax : list[start:stop:step]
* start : It is optional. Default value is 0.
* stop : It is mandatory. We have to specify the index from where we have to stop.
* step : It is optional. Default value is 1.
'''
# Example :
# l = [10,20,30,40,50,60,70,80,90]
# print(l[2:7:2])
# print(l[4:2])
# print(l[3:7])
# print(l[8:2:-1])
# print(l[4:100])
# Mutability : Once we create a list of object,we can modify its content. Hence list objects are mutable.
# Traversing : The sequential access of each element in the list is called as traversal.
# -------------------------------------------------------------------------------------------------------------------
# BY USING THE WHILE LOOP :
# n = [0,1,2,3,4,5,6,7,8,9,10]
# i = 0
# while i<len(n):
# print(n[i])
# i += 1
#---------------------------------------------------------------------------------------------------------------------
# 2 BY USING FOR-LOOP:
# n = [1,2,3,4,5,6,7,8,9,10]
# for i in n:
# print(i)
# l = ['A','B','C']
# n = len(l)
# for i in range(n):
# print(l[i],'is available at positive index: ',i, 'and negative index: ',i-n)
'''
---------------------------------------------------------------------------FUNCTIONS------------------------------------------------------------------------
1. len() : Returns no.of elements present in the list.
2. count() : It returns the no.of occurance of specified item in the list.
3. index() : It returns index of the first occurance of the specified element.
4. append() : To add item at the end of the list.
5. insert() : To insert an item at specified index position.
NOTE :
* If the specified index is greater than max index then element will be inserted at last position.
* If the specified index is smaller than min index then element will be inserted at first position.
***** Difference between append() and insert()? ***********
* append() : It adds item at the end of the list. That will be the last element.
* insert() : It adds item at specified index position. That will be the first element.
6. extend() : To add all elements of one list to another list.
7. remove() : To remove specified item from the list. If the item present multiple times
then only first occurance will be removed.
8. pop() : It removes and returns the last element of the list.
If the list is empty then pop() function raises the indexError.
Note :
* In general we can use append() and pop() function to implement stack data structure by using list , which follows LIFO[Last In First Out]
* n.pop(Index)===> To remove and return element present at specified index.
* n.pop()====> To remove and return last element of the list.
********** Difference between remove() and pop() ? ************************
POP :
----
* To remove specified element from the list.
* It can't return any value.
* If the specified element not available we will get valueError.
REMOVE :
---------
* To remove last element from the list.
* It returned removed element
* If list is empty then we will get an error:IndexError
Note :
* List objects dynamic, i.e based on our requirement we can increase and decrease the size.
* append(),insert(),extend()==>For increasing size/growable nature
* remove(),pop()==.For decreasing size/shrinking nature.
'''
# 1. len() :
# n = [1,2,4,5,7,8,'sun','pen']
# print(len(n))
# 2. count() :
# n = ['laptop' , 'python' , 100 , 10000 , True , 'laptop']
# print(n.count('laptop'))
# print(n.count(100))
# 3.index() :
# n = ['laptop' , 'python' , 100 , 10000 , True , 'laptop']
# print(n.index('laptop'))
# print(n.index(100))
# 4.append() :
# n = [10,20,30]
# n.append(50)
# n.append(100)
# print(n)
# To add all elements upto 100 which are divisible by 10.
# l = []
# for i in range(101):
# if i%10 == 0:
# l.append(i)
# print(l)
# 5.insert() :
# n = [1,2,3,4]
# n.insert(1,100)
# n.insert(-100 , 111)
# n.insert(-10 , 111)
# print('n=',n)
# 6. extend() :
# order1 = ['Chicken','Mutton','Fish']
# order2 = ['RC','KF','Boom Boom']
# order1.extend(order2)
# print(order1)
# Ex:
# order1 = ['Chicken','Mutton','Fish']
# order1.extend('Prawns')
# print(order1)
# 7. remove() :
# n = [10,20,30,10,20,10,20]
# n.remove(10)
# print(n)
# n = [10,20,30,10,20,10,20]
# n.remove(50)#ValueError: list.remove(x): x not in list
# l = [10,20,30,10,20]
# x = eval(input("Enter element to be removed: "))
# if x in l:
# l.remove(x)
# print("Removed successfully")
# else:
# print("Element not found")
# 8. pop() :
# l = [10,20,30,40]
# print(l.pop())#40
# print(l.pop())#30
# print(l)#[10,20]
# n = []
# print(n.pop()) # IndexError
# n = [10,20,1,2,30,40]
# print(n.pop())#40
# print(n.pop(1))#20
# print(n.pop(10))#IndexError: pop index out of range
# ------------------------------------------Ordering elements of the list ---------------------------------------------
# 1. reverse() :
# ---------------
# n = [20,10,40,30]
# n.reverse()
# print(n)
# 2. sort() :
# ------------
'''
* For numbers ===> default sorting order is ascending order.
* For strings ===> default order is alphabatocal order.
'''
# Ex
# n = [20,10,40,30]
# n.sort()
# print(n)#[10,20,30,40]
# n = ['sunny','lilly','radhika']
# n.sort()
# print(n)#['lilly', 'radhika', 'sunny']
# n = [10,'A',20,'B']
# n.sort() #'<' not supported between instances of 'str' and 'int'
# Descending order :
# ------------------
# n = [10,40,30,20]
# n.sort()
# print(n)
# n.sort(reverse=True)
# print(n)
# n.sort(reverse=False)
# print(n)
'''
Aliasing and cloning of list objects:
-----------------------------------------------------
-->The process of giving another reference variable to the existing list is called as aliasing.
-->The problem in this approach is by using one reference variable if we are changing content, then those changes will be reflected to the other reference variable.
-->To overcome this problem we should go for cloning.
-->The process of creating exactly duplicate independent object is called as cloning.
-->We can implement cloning by using slice operator or by using copy() function.
1).By using slice operator
2).By using copy() function
'='---> This is meant for the alising 'copy'--------> It is meant fot the cloning.
************************************ Difference between shallow copy and deep copy **********************************
'''
# x = [10,20,30,40]
# y = x #Aliasing
# y[1] = 333
# print('x:',x)#[10,333,30,40]
# print('y:',y)#[10,333,30,40]
# By using slice operator :
# x = [10,20,30,40]
# y = x
# y[1] = 333
# print('x:',x)
# print('y:',y)
# By using copy operator.
# x = [10,20,30,40]
# y = x.copy()
# y[1] = 333
# print('x:',x)
# print('y:',y)
'''
Difference between shallow cloning and deep cloning.
-----------------------------------------------------
1.SHALLOE CLONING :
-------------------
* It creates a new object,but inserts references into the objects found in the original.
* The new object is a different instance, any nested objects within it are still references to the same objects found in the original.
* Changes to nested objects affect both the original and the copy.
EX :
import copy
original = [1, 2, [3, 4]]
shallow_copy = copy.copy(original)
print("Original:", original)
print("Shallow Copy:", shallow_copy)
shallow_copy[2][0] = 99
print("After modifying the nested list:")
print("Original:", original)
print("Shallow Copy:", shallow_copy)
2. DEEP CLONING :
-----------------
* It creates a new objects and recursively copies all objects found in the original.
* All nested objects are duplicated as well, resulting in a completely independent copy.
* Changes to nested objects do not affect the original and vice versa
EX :
import copy
original = [1, 2, [3, 4]]
deep_copy = copy.deepcopy(original)
print("Original:", original)
print("Deep Copy:", deep_copy)
deep_copy[2][0] = 99
print("After modifying the nested list:")
print("Original:", original)
print("Deep Copy:", deep_copy)
'''
# Shallow copy
# import copy
# original = [1, 2, [3, 4]]
# shallow_copy = copy.copy(original)
# print("Original:", original)
# print("Shallow Copy:", shallow_copy)
# shallow_copy[2][0] = 99
# print("After modifying the nested list:")
# print("Original:", original)
# print("Shallow Copy:", shallow_copy)
# Deep copy
# import copy
# original = [1, 2, [3, 4]]
# deep_copy = copy.deepcopy(original)
# print("Original:", original)
# print("Deep Copy:", deep_copy)
# deep_copy[2][0] = 99
# print("After modifying the nested list:")
# print("Original:", original)
# print("Deep Copy:", deep_copy)
# using mathematical operators for list.
# 1. Concatentation : ----> We can use + to concatenate 2 list into a single list.
# a = [10,20,30]
# b = [40,50,60]
# c = a + b
# print(c)
# Ex :
# a = [10,20,30]
# b = [40,50,60]
# c = a.extend(b)
# print('a',a)
# print('b',b)
# print('c',c)
# Note: To use + operator both args should be list objects, otherwise we will get an error.
# 2. Repetition : ----> We can use * operator to repeat elements of the list.
# x = [10,20,30]
# y = x * 3
# print(y)
# Comparing list objects:
# ----------------------------------
# Ex:
x = [50,40,30]
y = [10,20,30,40,50,60]
print(x < y)#False
print(x > y)#True
# Membership operators:
# -->in
# -->not in
# Nested Lists:
# -------------------
# * Sometimes we can take one list inside another list. Such type of lists are called as nested list.
# n = [10,20,30,[40,50]]
# print(n)
# print(n[0])
# print(n[1])
# print(n[3][0])
# print(n[3][1])
# Note : We can access nested list elements by using index just like accessing multi dimensional array of elements.
# Nested list as matrix:
# --------------------------------
# In python we can represent matrix by using nested list.
# n = [[10,20,30],[40,50,60],[70,80,90]]
# print(n)
# print('Elements by row wise:')
# for r in n:
# print(r)
# print('Elements by matrix style:')
# for i in range(len(n)):#0,1,2
# for j in range(len(n[i])):#0,1,2
# print(n[i][j],end=' ')
# print()
# List Comprehensions:
# ===================
# It is very easy way of creating list objects from any iterable objects based on some condition.
# Syn:
# l = [expression for i in sequence if condition]
# Ex:print first 10-number squares in list format.
# 1st-way
# l = []
# for i in range(1,11):
# l.append(i*i)
# print(l)
# 2nd-way By using comprehension :
# l = [i*i for i in range(1,11)]
# print(l)
# Even numbers and odd numbers.
# l = [i for i in range(1,11) if i%2==0]
# print("Even square numbers are : ", l)
# o = [i*i for i in range(1,11) if i%2 !=0]
# print("Odd square numbers are : ", o)
# Ex:
# word = 'the quick brown fox jums over the lazy dog'
# o/p:[['THE',3],['QUICK',5],['BROWN',5],......['DOG',3]]
# word = 'the quick brown fox jums over the lazy dog'
# words = word.split()
# l=[[w.upper(),len(w)] for w in words]
# print(l)
# w.a.p to display unique vowels present in the given string
# --------------------------------------------------------------------------------------
word = input('Enter the word to search for vowels:')
vowels = ['a','e','i','o','u']
found = [ ]
for letter in word:
if letter in vowels:
if letter not in found:
found.append(letter)
print(found)