-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython 38 dictionary
More file actions
73 lines (67 loc) · 2.2 KB
/
Copy pathPython 38 dictionary
File metadata and controls
73 lines (67 loc) · 2.2 KB
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
#Create cities abbreviation
Cities = {
'Greater London': 'GL',
'Manchester': 'MC',
'Birmingham': 'BR',
'Leeds': 'LE',
'Nottingham': 'NO'
}
#Create boroughs with cities
Boroughs = {
'GL': 'Islington',
'GL': 'Hackney',
'LE': 'Walton'
}
Boroughs['NO'] = 'Tower Hamlets'
Boroughs['MC'] = 'Bromley'
#Print out some cities
print '_'*10
print "England has many boroughs: ", Boroughs['NO']
print "or ", Boroughs['MC']
print "These two are not the best boroughs"
print'_'*10
print "Leeds abbreviation is:", Cities['Leeds']
print "Nottingham's abbreviation is:", Cities['Nottingham']
# do it by using the state then cities dict
print'_'*10
print "Greater London: ", Boroughs[Cities['Greater London']]
print "Greater London: ", Boroughs[Cities['Greater London']]# prints hackney twice
print "Leeds: ", Boroughs[Cities['Leeds']]
#print every cities abbreviation
#the below did not work - yet the one underworked
#print '_' * 10
#for city, abbrev in Cities.item():
# print "%s is abbreviated %s" % (city, abbrev)
print '-' * 10
for city, abbrev in Cities.items():
print "%s is abbreviated %s" % (city, abbrev)
#The below did not work either,had to copy and paste from the site
#print '_' * 10
#for abbrev, Boroughs in Boroughs.item():
#print "s is has the boroughs %s" % (abbrev, Boroughs)
print '-' * 10
for abbrev, Boroughs in Boroughs.items():
print "%s has the Boroughs %s" % (abbrev, Boroughs)
##does not work
#print '-' * 10
#for city, abbrev in Cities.items():
# print "%s city is abbreviated %s and has Boroughs %s" % (
# city, abbrev, Boroughs[abbrev])
#print '-' * 10
#for state, abbrev in states.items():
# print "%s state is abbreviated %s and has city %s" % (
#state, abbrev, cities[abbrev])
# now do both at the same time
#print '-' * 10
#for city, abbrev in Cities.items():
# print "%s Cities is abbreviated %s and has Boroughs %s" % (
#Cities, abbrev, Boroughs[abbrev])
print '-' * 10
# safely get a abbreviation by state that might not be there
city = Cities.get('Norwich', None)
if not city:
print "Sorry, no Norwich."
#NOT WORKING
# get a borough with a default value
borough = Boroughs.get('NW', 'Does Not Exist')
print "The borough for the city 'NW' is: %s" % borough