L. Jones
L. Jones

Reputation: 23

Sorting a Dictionary with Tuples

So, for fun and practice I have been following along a college friend and their more advanced programming class and tackling assignments they are assigned to ready myself for the same Python class. I'm now at dictionaries and tuples sorting them based on the least to greatest number in an assigned dictonary. so far I have this code:

cityRevenues = {'Alabaster':[40,50,23,18], 'Anniston':[56,78,34,11], 
               'Athens':[40,34,18,30],'Auburn':[55,67,23,11],
               'Decatur':[44,23,56,11],'Florence':[55,67,33,23],'Gadsden':[45,67,54,77]}

a = (sorted(cityRevenues.items(), key=lambda x: x[1]))
print('Sort the cities by their Quarter 1 Revenues.')
print("")
print(a)
print("")
b = (sorted(cityRevenues.items(), key=lambda x: x[1][1]))
print('Sort the cities by their Quarter 2 Revenues.')
print("")
print(b)
print("")
c = (sorted(cityRevenues.items(), key=lambda x: x[1][2]))
print("Sort the cities by their Quarter 3 Revenues.")
print("")
print(c)
print("")
d = (sorted(cityRevenues.items(), key=lambda x: x[1][3]))
print("Sort the cities by their Quarter 4 Revenues.")
print("")
print(d)
print("")

which gives the output:

Sort the cities by their Quarter 1 Revenues.

[('Athens', [40, 34, 18, 30]), ('Alabaster', [40, 50, 23, 18]), ('Decatur', [44, 23, 56, 11]), ('Gadsden', [45, 67, 54, 77]), ('Auburn', [55, 67, 23, 11]), ('Florence', [55, 67, 33, 23]), ('Anniston', [56, 78, 34, 11])]

Sort the cities by their Quarter 2 Revenues.

[('Decatur', [44, 23, 56, 11]), ('Athens', [40, 34, 18, 30]), ('Alabaster', [40, 50, 23, 18]), ('Auburn', [55, 67, 23, 11]), ('Florence', [55, 67, 33, 23]), ('Gadsden', [45, 67, 54, 77]), ('Anniston', [56, 78, 34, 11])]

Sort the cities by their Quarter 3 Revenues.

[('Athens', [40, 34, 18, 30]), ('Auburn', [55, 67, 23, 11]), ('Alabaster', [40, 50, 23, 18]), ('Florence', [55, 67, 33, 23]), ('Anniston', [56, 78, 34, 11]), ('Gadsden', [45, 67, 54, 77]), ('Decatur', [44, 23, 56, 11])]

Sort the cities by their Quarter 4 Revenues.

[('Auburn', [55, 67, 23, 11]), ('Decatur', [44, 23, 56, 11]), ('Anniston', [56, 78, 34, 11]), ('Alabaster', [40, 50, 23, 18]), ('Florence', [55, 67, 33, 23]), ('Athens', [40, 34, 18, 30]), ('Gadsden', [45, 67, 54, 77])]

I managed to sort them based on the least to greatest in each tuple, but I do not know how to code it to where it shows only the one value that is greatest in the tuple. i.e.:

Sort the cities by their Quarter 1 Revenues.

[('Athens', [40]), ('Alabaster', [40]), ('Decatur', [44]), ('Gadsden', [45]), ('Auburn', [55]), ('Florence', [55]), ('Anniston', [56])]

How would I go about having the specific tuple values be the only ones printed?

Upvotes: 0

Views: 89

Answers (3)

Onel Harrison
Onel Harrison

Reputation: 1334

Try the following:

# To get the first value of the list for the city, use pair[1][0].
# You can get the second value of the list for the city by using pair[1][1]
# An so on...

>>> d = {pair[0]: [pair[1][0]] for pair in sorted(cityRevenues.items(), key=lambda x: x[1][3])}
>>> list(d.items())
[('Gadsden', [45]), ('Athens', [40]), ('Anniston', [56]), ('Alabaster', [40]), ('Florence', [55]), ('Auburn', [55]), ('Decatur', [44])]

Upvotes: 1

Prune
Prune

Reputation: 77837

You print the last item in the list:

print (a[-1])

Format as desired.

By the way, you can make this code much shorter if you use the same variable name for each sorted list, and also collect your keys in a list you can index according to the input choice.

Upvotes: 0

Anthony Kong
Anthony Kong

Reputation: 40624

Since the largest element is at the end of the list, you can use index -1

e.g.

cityRevenues = {'Alabaster':[40,50,23,18], 'Anniston':[56,78,34,11], 
               'Athens':[40,34,18,30],'Auburn':[55,67,23,11],
               'Decatur':[44,23,56,11],'Florence':[55,67,33,23],'Gadsden':[45,67,54,77]}

a = (sorted(cityRevenues.items(), key=lambda x: x[1]))
print('Sort the cities by their Quarter 1 Revenues.')
print("\n")
print(a[-1])
print("\n")

It will give

('Anniston', [56, 78, 34, 11])

Upvotes: 0

Related Questions