This program sorts the number of Olympic Medals won by each nation in descending order.
Countries and numbers of Medals are stored in a dictionary
This is what the output looks like (the screenshot shows only half of the list, but you get the picture):
Considered that this program basically only needed four lines of code it took me a long while to figure it out.
I wanted to sort the dictionary by its value: The number of medails. In the beginning the numbers in the
dictionary (that I got from Wikipedia) were stored as strings like this:
medails = {
"Country A" = "1",
"Country B" = "8",
"Country C" = "11
}
It turned out that it would sort „1, „11“ and than „8“ – well because it is a string and not a number.
So I tried to store one medail number as an Integer, just to try it out but that throwed me an error. It turned out I had to turn all strings
into Integers first and then it would sort properly – even though the Python-Docs say you can mix any sort of values in a dictionary.
The program itself looks like this:
from medails_dictionary import medails # thats where I stored the dictionary
# sort the dictionary by its number values in decending order
c = sorted(medails.items(),key=lambda x: x[1], reverse=True)
# go through every key and print the key-value-pair
for x in c:
print (x)
The dictionary looks like this:
medails = {
"Afghanistan" : 2,
"Algeria" : 17,
"Argentina" : 74,
"Armenia" : 14,
"Australasia" : 12,
"Australia" : 509,
"Austria" : 305,
"Azerbaijan": 43,
"Bahamas" : 14,
"Bahrain" : 3,
"Barbados" : 1,
"Belarus" : 93,
"Belgium" : 153,
"Bermuda" : 1,
"Bohemia" : 4,
"Botswana" : 1,
"Brazil" : 128,
"British West Indies" : 2,
"Bulgaria" : 224,
"Burundi" : 2,
"Cameroon" : 6,
"Canada" : 472,
"Chile" : 13,
"China" : 599,
"Chinese Taipei" : 24,
"Colombia" : 28,
"Costa Rica" : 4,
"Croatia" : 44,
"Cuba" : 225,
"Cyprus" : 1,
"Czech Republic" : 80,
"Czechoslovakia" : 168,
"Denmark" : 195,
"Djibouti" : 1,
"Dominican Republic" : 7,
"East Germany" : 519,
"Ecuador" : 2,
"Egypt" : 32,
"Eritrea" : 1,
"Estonia" : 41,
"Ethiopia" : 53,
"Fiji" : 1,
"Finland" : 464,
"France" : 825,
"Gabon" : 1,
"Georgia" : 33,
"Germany" : 824,
"Ghana" : 4,
"Great Britain" : 875,
"Greece" : 116,
"Grenada" : 2,
"Guatemala" : 1,
"Guyana" : 1,
"Haiti" : 2,
"Hong Kong" : 3,
"Hungary" : 497,
"Iceland" : 4,
"Independent Olympic Athletes" : 2,
"Independent Olympic Participants" : 3,
"India" : 28,
"Indonesia" : 32,
"Iran" : 69,
"Iraq" : 1,
"Ireland" : 31,
"Israel" : 9,
"Italy" : 691,
"Ivory Coast" : 3,
"Jamaica" : 77,
"Japan" : 484,
"Jordan" : 1,
"Kazakhstan" : 69,
"Kenya" : 102,
"Kosovo" : 1,
"Kuwait" : 2,
"Kyrgyzstan" : 4,
"Latvia" : 26,
"Lebanon" : 4,
"Liechtenstein" : 9,
"Lithuania" : 25,
"Luxembourg" : 4,
"Macedonia" : 1,
"Malaysia" : 11,
"Mauritius" : 1,
"Mexico" : 69,
"Mixed Team" : 17,
"Moldova" : 5,
"Mongolia" : 26,
"Montenegro" : 1,
"Marocco" : 23,
"Mozambique" : 2,
"Namibia" : 4,
"Netherlands Antilles" : 1,
"Netherlands" : 395,
"New Zealand" : 118,
"Nigeria" : 25,
"Niger" : 2,
"North Korea" : 56,
"Norway" : 481,
"Pakistan" : 10,
"Panama" : 3,
"Paraguay" : 1,
"Peru" : 4,
"Philippines" : 10,
"Poland" : 304,
"Portugal" : 24,
"Puerto Rico" : 9,
"Qatar" : 5,
"Romania" : 307,
"Russian Empire" : 8,
"Russia" : 537,
"Samoa" : 1,
"Saudi Arabia" : 3,
"Senegal" : 1,
"Serbia and Montenegro" : 9,
"Serbia" : 15,
"Singapore" : 5,
"Slovakia" : 33,
"Slovenia" : 38,
"South Africa" : 86,
"South Korea" : 320,
"Soviet Union" : 1204,
"Spain" : 152,
"Sri Lanka" : 2,
"Sudan" : 1,
"Suriname" : 2,
"Sweden" : 638,
"Switzerland" : 330,
"Syria" : 3,
"Tajikistan" : 4,
"Tanzania" : 2,
"Thailand" : 33,
"Togo" : 1,
"Tonga" : 1,
"Trinidad and Tobago" : 19,
"Tunisia" : 13,
"Turkey" : 91,
"Uganda" : 7,
"Ukraine" : 128,
"Unified Team" : 135,
"United Arab Emirates" : 2,
"United States" : 2804,
"United Team of Germany" : 137,
"Uruguay" : 10,
"Uzbekistan" : 32,
"Venezuela" : 15,
"Vietnam" : 4,
"Virgin Islands" : 1,
"West Germany" : 243,
"Yugoslavia" : 87,
"Zambia" : 2,
"Zimbabwe" : 8
}