Friday, July 27, 2018

Uses of List_Session 5

print("Hello to Data Structures")
Hello to Data Structures
In [2]:
pList1 = [1,'I am a boy']
print (pList1)
[1, 'I am a boy']
In [3]:
pInt =2
pReal =3.414
pString ="Praxis Business School "
pList2 =[pInt, pReal, pString, pList1]
print (pList2)
[2, 3.414, 'Praxis Business School ', [1, 'I am a boy']]
In [6]:
print ("member------")
print (pList2[0])
print (pList2[1])
print (pList2[2])
print (pList2[3])
print (pList2[3][0])
print (pList2[3][1])
member------
2
3.414
Praxis Business School 
[1, 'I am a boy']
1
I am a boy
In [7]:
#methods operation on list
print ("methods-----")
pList1.append(pInt)
print (pList1)
pList1.insert(1,pReal)
print (pList1)
pList1.remove (pReal)
print (pList1)
pList1.extend (pList2)
print (pList1)
methods-----
[1, 'I am a boy', 2]
[1, 3.414, 'I am a boy', 2]
[1, 'I am a boy', 2]
[1, 'I am a boy', 2, 2, 3.414, 'Praxis Business School ', [...]]
In [11]:
#few more methods
print ("more methods")
print (pList1.count("I am a boy"),pList1.count(pInt))
print (pList1.index(pInt), pList1.index("I am a boy"))
pList1.reverse ()
print (pList1)
list1 = [10,12,9,11,7,6,5,14,40,20]
list1.sort ()
print (list1)
more methods
1 2
3 5
[1, 'I am a boy', 2, 2, 3.414, 'Praxis Business School ', [...]]
[5, 6, 7, 9, 10, 11, 12, 14, 20, 40]
In [20]:
#list as a Stack
print ("stack......")
pStack =[12,20,10]
pStack.append(30)
print (pStack)
pPopped=pStack.pop ()
print (pPopped ,pStack)
#pPush=pStack.push (42)
#print (pPush)
stack......
[12, 20, 10, 30]
30 [12, 20, 10]
In [3]:
#list as queue
print("Q -----")

from collections import deque
pList3 =["Ram","Shyam","Mohan"]
pQ= deque(pList3)
print (pQ)
a=pQ.pop()
print(a)
print (pQ)
b=pQ.pop()
print(b)
print (pQ)
pQ.append("Sita")
print (pQ)
Q -----
deque(['Ram', 'Shyam', 'Mohan'])
Mohan
deque(['Ram', 'Shyam'])
Shyam
deque(['Ram'])
deque(['Ram', 'Sita'])
In [43]:
#sets
print ("Sets.............")
pList5=["apple","mango","orange","apple","lemon"]
set2={"water lemon","graps","pineapple","Mango"}
print (pList5)
print (set2)
pSet =set(pList5)
print (pSet)
pSet1 = pSet.union(set2)
print (pSet1)
#pSet2= pSet.intersection(set2)
#print (pSet2)
Sets.............
['apple', 'mango', 'orange', 'apple', 'lemon']
{'graps', 'pineapple', 'Mango', 'water lemon'}
{'orange', 'mango', 'apple', 'lemon'}
{'pineapple', 'lemon', 'graps', 'water lemon', 'orange', 'Mango', 'mango', 'apple'}
In [49]:
#tuples
print ("Tuples........")
pTuple1 = 15,10,"Ram","Shyam"
print (pTuple1)
print (pTuple1[0],pTuple1[3])
pTuple2 = 20, "Praxis Business School", pTuple1
print(pTuple2)
Tuples........
(15, 10, 'Ram', 'Shyam')
15 Shyam
(20, 'Praxis Business School', (15, 10, 'Ram', 'Shyam'))
In [67]:
# Dictionaries

print("Dictionaries ----------")

dTel = { "Ranjan": 1992, "Raushan" : 1985, "Rashmi" : 1988, "Gaurav" : 1980,"Gudiya" : 1994}
print(dTel)
print (dTel.keys())
print (dTel.values())
dTel ['Raj'] =1959
dTel ['Sobha']=1962
print (dTel)
dTel['Ranjan']=1990
print (dTel)
print ("Ranjan" in dTel)
print ("ranjan" in dTel)
Dictionaries ----------
{'Ranjan': 1992, 'Raushan': 1985, 'Rashmi': 1988, 'Gaurav': 1980, 'Gudiya': 1994}
dict_keys(['Ranjan', 'Raushan', 'Rashmi', 'Gaurav', 'Gudiya'])
dict_values([1992, 1985, 1988, 1980, 1994])
{'Ranjan': 1992, 'Raushan': 1985, 'Rashmi': 1988, 'Gaurav': 1980, 'Gudiya': 1994, 'Raj': 1959, 'Sobha': 1962}
{'Ranjan': 1990, 'Raushan': 1985, 'Rashmi': 1988, 'Gaurav': 1980, 'Gudiya': 1994, 'Raj': 1959, 'Sobha': 1962}
True
False
In [70]:
#loop
print ("loop............")
for key, values in dTel.items():
    print (key,values)
loop............
Ranjan 1990
Raushan 1985
Rashmi 1988
Gaurav 1980
Gudiya 1994
Raj 1959
Sobha 1962
In [75]:
print("Enumerate .........")
for ix, val in enumerate (pList1):
    print (ix,val)
    
for ix, val in enumerate (pTuple1):
    print (ix,val)
    
for ix, val in enumerate (dTel):
    print (ix,val)
Enumerate .........
0 1
1 I am a boy
2 2
3 2
4 3.414
5 Praxis Business School 
6 [1, 'I am a boy', 2, 2, 3.414, 'Praxis Business School ', [...]]
0 15
1 10
2 Ram
3 Shyam
0 Ranjan
1 Raushan
2 Rashmi
3 Gaurav
4 Gudiya
5 Raj
6 Sobha
In [78]:
print ("Zip........")
teachers = ["prithwis","charan","jaydeep","subhasis"]
subjects = ["bigdata", "communications", "stats", "datamining"]
for t, s in zip (teachers, subjects):
    print (t, "teaches", s)
Zip........
prithwis teaches bigdata
charan teaches communications
jaydeep teaches stats
subhasis teaches datamining
In [87]:
# Membership

print(pInt in pList2)
print(2 in pList2)
print(3.414 not in pList2)
fruit1 = ["apple", "pear", "orange", "mango"]
fruit2 = ["apple", "grape", "banana","mango"]
print ("grape" in fruit1)
print ("banana" in fruit2)
print (set(fruit1).intersection(fruit2))
print (set(fruit1).union(fruit2))
True
True
False
False
True
{'mango', 'apple'}
{'pear', 'orange', 'grape', 'mango', 'banana', 'apple'}
In [98]:
#delete
a= [-2,3,5,79,1,34,-7]
print (a)
a.sort()
print (a)
del a[0]
print (a)
#del a[6]
#print (a)
del a[2:4]
print (a)
del a[:]
print (a)
[-2, 3, 5, 79, 1, 34, -7]
[-7, -2, 1, 3, 5, 34, 79]
[-2, 1, 3, 5, 34, 79]
[-2, 1, 34, 79]
[]