-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastructures.py
140 lines (117 loc) · 3.4 KB
/
datastructures.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
str1 = 'Hello Students' #string str1
str2 = ' how are you' #string str2
str1
str2
print (str1[0:5]) #printing first five character using slice operator
(str1[0:5])
print (str1[4]) #printing 5th character of the string
print (str1*2) #printing the string twice
print (str1 + str2) #printing the concatenation of str1 and str2
#Lists
l = [1, "hi", "python", True]
print (l[3:])
print (l[0:2])
print (l);
print (l + l);
print (l * 3);
print (type(l))
#Lets try mutation
l[1] = "Bye"
print (l)
#Tuple
t = ('hi', 'python', 2, 4)
t
print (t[1:]);
print (t[0:2]);
print (t);
print (t + t)
print (t * 3)
print (type(t))
#Lets try mutation
t[1] = "Bye"
print (t)
#Dictionary
d = {1:"Jimmy", 2:'Alex', 3:'john', 4:'mike'}
d
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print (d);
print (d.keys());
print (d.values());
#----ADVANCED----
#list
#ordered collection of items; sequence of items in a list
shoplist =['apple','carrot','mango', 'banana']
shoplist
len(shoplist)
print(shoplist)
#add item to list
shoplist.append('rice')
shoplist
#sort
shoplist.sort() #inplace sort
shoplist
#index/select
shoplist[0]
shoplist[0:4]
#delete item
del shoplist[0]
shoplist
#Tuple
#Used to hold multiple object; similar to lists; less functionality than list
#immutable - cannot modify- fast ; ( )
zoo = ('python','lion','elephant','bird')
zoo
len(zoo)
languages = 'c', 'java', 'php' #better to put (), this also works
languages
#Dictionary - like an addressbook. use of associate keys with values
#key-value pairs { 'key1':value1, 'key2':value2} ; { } bracket, :colon
student = {'A101': 'Abhinav', 'A102': 'Rohit', 'A103':'Rahul', 'A104': 'Karan'}
student
student['A103']
print('Name of rollno A103 is ' +student['A103'])
del student['A104']
student
len(student)
#for rollno, name in student.items():
#print('name of {} is {} '.format(rollno, name) )
#Lets test Mutation:
#adding a value
student['A104'] = 'Hitesh'
student
#Set
#Sets are unordered collections of objects; ( [ , ])
teamA = set(['india','england','australia','sri lanka','ireland'])
teamA
teamB = set(['pakistan', 'south africa','bangladesh','ireland'])
teamB
#Checking whether a data value exists in a set or not.
'india' in teamA
'india' in teamB
#Adding values in a set
teamA.add('china')
teamA #puts in order
teamA.add('india')
teamA #no duplicates
teamA.remove('australia')
teamA
#Create dataframe :
import pandas as pd
#Create a DataFrame
d = {'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine',
'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
'Exam':['Semester 1','Semester 1','Semester 1','Semester 1','Semester 1','Semester 1',
'Semester 2','Semester 2','Semester 2','Semester 2','Semester 2','Semester 2'],
'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science',
'Mathematics','Mathematics','Mathematics','Science','Science','Science'],
'Score':[62,47,55,74,31,77,85,63,42,67,89,81]}
d
df = pd.DataFrame(d,columns=['Name','Exam','Subject','Score'])
df
#View a column of the dataframe in pandas:
df['Name']
#View two columns of the dataframe in pandas:
df[['Name','Score','Exam']]
#View first two rows of the dataframe in pandas:
df[0:2]