COMP 204
Dictionaries Mathieu Blanchette, based on material from Carlos Oliver Gonzalez and Christopher Cameron
1 / 23
COMP 204 Dictionaries Mathieu Blanchette, based on material from - - PowerPoint PPT Presentation
COMP 204 Dictionaries Mathieu Blanchette, based on material from Carlos Oliver Gonzalez and Christopher Cameron 1 / 23 Note about two-dimensional lists In your assignment #2, you will need to represent two-dimensional tables, with a fixed
1 / 23
1
2
3
4 5
6 7 # to
8 # s p e c i f y
9
10
2 / 23
1 # A p o s i t i o n
2 PFM = [
3
4
5
6 7 PFM[ 0 ] [ 2 ] = PFM[ 3 ] [ 2 ] + PFM[ 2 ] [ 4 ] 8
3 / 23
1 # C r e a t i n g
2 # f i l l e d
3 nrows = 4 4 n c o l s = 5 5 PFM = [ [ 0
6
4 / 23
5 / 23
6 / 23
7 / 23
8 / 23
9 / 23
10 / 23
11 / 23
12 / 23
1 my dict =
13 / 23
1 # t h i s
2 p e r i o d i c T a b l e = {”H” : 1 ,
3 4 elementsCodes = {”Hydrogen ” : ”H” ,
5
14 / 23
1 # t h i s
2 p e r i o d i c T a b l e = {”H” : 1 ,
3 4 elementsCodes = {”Hydrogen ” : ”H” ,
5
6 7 mass = p e r i o d i c T a b l e [ ”K” ] 8 9 10 p e r i o d i c T a b l e [ ”He” ] = 4
11 p e r i o d i c T a b l e [ ”Na”]= 23
12 13 #p e r i o d i c T a b l e
14 15 p e r i o d i c T a b l e [ ”C” ] = 12.01
16 17 d e l
15 / 23
1 # t h i s
2 p e r i o d i c T a b l e = {”H” : 1 ,
3 4 elementsCodes = {”Hydrogen ” : ”H” ,
5
6 7 mass = p e r i o d i c T a b l e [ ”K” ] 8 9 10 p e r i o d i c T a b l e [ ”He” ] = 4
11 p e r i o d i c T a b l e [ ”Na”]= 23
12 13 #p e r i o d i c T a b l e
14 15 p e r i o d i c T a b l e [ ”C” ] = 12.01
16 17 d e l
16 / 23
1 # a
2 mixedDict = {”H” : ” Hydrogen” ,
3
4 5 6 product = mixedDict [ ( 4 , 5 ) ]
7 primeFactors = mixedDict [ 3 0 ]
8 9 f a c = mixedDict [ 2 0 ]
17 / 23
1 # a
2 p e r i o d i c T a b l e = {”H” :
3
4
5
6 7 carbonDic = p e r i o d i c T a b l e [ ”C” ]
8 m = carbonDic [ ”mass” ]
9 10 #or
11 m = p e r i o d i c T a b l e [ ”C” ] [ ”mass” ]
18 / 23
1 per = {”H” : 1 ,
2 3 k e y L i s t = l i s t (
4 # Note :
5 #
6 #
7 8 f o r
9
1 per = {”H” : 1 ,
2 3 i t e m L i s t = l i s t (
4 # Note :
5 #
6 #
7 8 # i t e m L i s t
9 # [ ( ’H ’ ,
10 11 f o r
12
19 / 23
1
2 3 newElement = ”Na” 4
5
6
7
1 per = {”H” : 1 ,
2 newTable = {”Na” : 2 3 ,
3 4 # Add the
5 per . update ( newTable ) # per now has
6
20 / 23
1 sequence = ” H e l l o my name
2
3 counts = {}
4 f o r
5
6
7
8
9
10 11
21 / 23
1 p e r i o d i c T a b l e = {”H” : 1 ,
2 3 a c e t i c A c i d = ”CHHHCOOH” 4 5 mass = 0 6 f o r
7
8 9
22 / 23
1 p e r i o d i c T a b l e = {”H” : 1 ,
2 3 molecules = {”Carbon
4
5
6 7 moleculeMass = {}
8
9 f o r
10
11
12
13
14 15
23 / 23