CSC111 List of Tuples Demo
--D. Thiebaut (talk) 09:58, 12 April 2015 (EDT)
# Lab11Demo.py
# D. Thiebaut
# Demonstration Program for processing
# lists presented in textual information, and creating
# a list of tuples from the different lines.
# The list of tuples can easily be sorted in increasing or decreasing
# order, once the information is split into fields and made into
# a list of tuples.
# This program uses a list of countries and the Internet speed available for its citizens.
# This information is reported in Wikipedia. We use this list that has been artificially
# shuffled to get the alphabetical listing by country name, the top-10 countries by fastest
# Internet access, and the lowest 10 countries by slowest access.
# The list from Wikipedia is copy/pasted in a string called text.
# http://en.wikipedia.org/wiki/List_of_countries_by_Internet_connection_speeds
text = """ Canada 10.3
Switzerland 14.5
Uruguay 5.5
Taiwan 9.5
Romania 11.3
Brazil 2.9
Germany 8.7
Russia 9.1
Bolivia 1.1
Venezuela 1.3
Israel 11.4
Indonesia 3.7
Ireland 13.9
New Zealand 7.0
India 2.0
South Africa 3.6
United Arab Emirates 4.7
Portugal 8.0
Denmark 11.2
Austria 10.4
Australia 6.9
Norway 11.4
Finland 11.7
Panama 2.9
France 6.9
China 3.8
Japan 15.0
Czech Republic 12.3
Paraguay 1.3
Argentina 4.2
Costa Rica 2.7
Chile 4.1
Slovakia 8.6
Turkey 5.5
Belgium 11.4
Netherlands 14.0
Italy 5.5
Singapore 12.2
United States 11.5
Mexico 4.1
Sweden 14.1
United Kingdom 10.7
Hungary 8.8
Colombia 3.4
Spain 7.8
Peru 3.6
South Korea 25.3
Ecuador 3.6
Vietnam 2.5
Thailand 6.6
Poland 8.6
Hong Kong 16.3
Malaysia 4.1
Philippines 2.5"""
def main():
# =========================================================
# print the list by countries, alphabetically
# =========================================================
print( "\n\n=== ALPHABETICAL LISTING ===" )
# prepare an empty list
list = []
# split the text into lines...
for line in text.split( "\n" ):
# split each line into words
fields = line.split()
# skip lines that do not have at least 2 fields
if len( fields ) < 2:
continue
# create a tuple with country name and speed, sorted alphabetically
country = " ".join( fields[0:-1] )
speed = float( fields[-1] )
tuple =( country, speed )
# add tupple to list
list.append( tuple )
# loop over. Sort it alphabetically
list.sort()
# display the countries alphabetically
for country, speed in list:
print( "{0:20} {1:4.1f}".format( country, speed ) )
# =========================================================
# print the list by speed, top 10 only
# =========================================================
print( "\n\n=== TOP-10 BY SPEED ===" )
list2 = []
for country, speed in list:
tuple = (speed, country )
list2.append( tuple )
# sort by speed (since it's the first item in the tuples)
list2.sort()
# sort in decreasing order by reversing
list2.reverse()
for i in range( min( len( list2), 10 ) ):
speed, country = list2[i]
print( "{0:20} {1:4.1f}".format( country, speed ) )
# =========================================================
# print the list by speed, lowest 10 only
# =========================================================
print( "\n\n=== LAST-10 BY SPEED ===" )
list2 = []
for country, speed in list:
tuple = (speed, country )
list2.append( tuple )
# sort by speed (since it's the first item in the tuples)
list2.sort()
for i in range( min( len( list2), 10 ) ):
speed, country = list2[i]
print( "{0:20} {1:4.1f}".format( country, speed ) )
main()