-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.py
58 lines (51 loc) · 1.56 KB
/
Dictionary.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
"""Time to play with Python dictionaries!
You're going to work on a dictionary that
stores cities by country and continent.
One is done for you - the city of Mountain
View is in the USA, which is in North America.
You need to add the cities listed below by
modifying the structure.
Then, you should print out the values specified
by looking them up in the structure.
Cities to add:
Bangalore (India, Asia)
Atlanta (USA, North America)
Cairo (Egypt, Africa)
Shanghai (China, Asia)"""
locations = {
'North America': {
'USA': ['Mountain View','Atlanta']
},
'Asia': {
'India':['Bangalore'],
'China':['Shangai']
},
'Africa': {
'Egypt':['Cairo']
}
}
"""Print the following (using "print").
1. A list of all cities in the USA in
alphabetic order.
2. All cities in Asia, in alphabetic
order, next to the name of the country.
In your output, label each answer with a number
so it looks like this:
1
American City
American City
2
Asian City - Country
Asian City - Country"""
print '1'
usa_sorted = sorted(locations['North America']['USA'])
for city in usa_sorted:
print city
print 2
asia_cities = []
for countries, cities in locations['Asia'].items():
city_country = cities[0] + " - " + countries
asia_cities.append(city_country)
asia_sorted = sorted(asia_cities)
for city in asia_sorted:
print city