Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python help

Name: bob 2011-03-22 13:12

So im using python (an older version) and i have a list of country names and an associated numbers, I need to sort these countries from highest to lowest in terms of that number,

Also I need to associate this list with another list, and there are some countries in the second list that are not in the first... durr?????

Name: NO EXCEPTIONS 2011-03-22 13:57

>>1

>>> countries = [('USA', 2), ('Murka', 1), ('Merika', 11), ('United States', 0)]
>>> countries
[('USA', 2), ('Murka', 1), ('Merika', 11), ('United States', 0)]

>>> sorted_countries = sorted(countries, key = lambda pair: pair[1])
>>> sorted_countries
[('United States', 0), ('Murka', 1), ('USA', 2), ('Merika', 11)]

>>> additional_data = {'United States': '123123', 'Best Korea': '123123213213'}
>>> extended_countries = [(name, value, additional_data.get(name, '')) for name, value in sorted_countries]
>>> extended_countries
[('United States', 0, '123123'), ('Murka', 1, ''), ('USA', 2, ''), ('Merika', 11, '')]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List