-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample7.py
More file actions
39 lines (32 loc) · 1.17 KB
/
Copy pathexample7.py
File metadata and controls
39 lines (32 loc) · 1.17 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
# Example 7
def main():
# Create a list that contains data about countries.
countries = [
# [country_name, land_area, population, gdp_per_capita]
["Mexico", 1972550, 126014024, 21362],
["France", 640679, 67399000, 45454],
["Ghana", 239567, 31072940, 7343],
["Brazil", 8515767, 210147125, 14563],
["Japan", 377975, 125480000, 41634]
]
# Print the unsorted list.
print("Original unsorted list of countries")
for country in countries:
print(country)
print()
# Define a lambda function that will be used as the
# key function by the sorted function. The lambda
# function extracts the population data from a
# country so that the population will be used for
# sorting the list of countries.
POPULATION_INDEX = 2
popul_func = lambda country: country[POPULATION_INDEX]
# Sort the list of countries by the population.
sorted_list = sorted(countries, key=popul_func)
# Print the sorted list.
print("List of countries sorted by population")
for country in sorted_list:
print(country)
# Call main to start this program.
if __name__ == "__main__":
main()