0

I don't want to sort a collection of lists according to a selected list but rather, I have a dictionary which contains lists. I wish to display the dictionary after sorting the lists according to a chosen key.

names = ["John", "Mary", "Frank", "Sandra"]
countries= ["France", "Spain", "Holland", "America"]
travel_days = [23, 14, 52, 21]
places_travelled = [3, 7, 6, 12]

dictionaryIs = {"Names": names, "Countries":countries, "Days":travel_days, "Places", places_travelled}

So from these entries, John is in France, has been there for 23 days and has seen 3 places. I am trying to do ordered searches by ascending and descending order, such as "Who has travelled the longest time? Who has travelled to the fewest places?"

sorted(x.values())

This sorted method on sorts only "John", "France", 23, 3. With the other lists following the same order

sorted(x.items(), key = lambda e:e[1][2])

This method sorts "Mary", "Spain", 14, 7. Though I'm not sure how. But isn't what I'm looking for.

After sorting travel_days to look like [52, 34, 21, 14], the other list should match the reordering. How do you order a dictionary of lists by key values ascending and descending order?

gavin
  • 305
  • 1
  • 2
  • 12
  • YesI see my typo in the code of "Places", places_travelled. This doesn't reflect my actual code – gavin Mar 26 '17 at 06:12
  • Where does `34` comes from? – falsetru Mar 26 '17 at 06:13
  • I can't even figure out what `dictionaryIs` is supposed to be or do. – TigerhawkT3 Mar 26 '17 at 06:16
  • dictionaryIs is the name of my dictionary which is filled with lists. The first key item "Names" is filled with the list called names John, Mary, Frank, Sandra. As for 34...I really should have vetted my own question better. It should have been Mary, Spain, 14, 7 – gavin Mar 26 '17 at 08:13
  • Adding in all the dictionary keys as a zip was the trick to this – gavin Mar 30 '17 at 05:06
  • Adding in all the dictionary keys as a zip was the trick to this sortBy = 3 sortedRes = sorted(zip(dictionaryIs["Names"], dictionaryIs["Countries"], dictionaryIs["Days"], dictionaryIs["Places"]), key=lambda x: x[sortBy], reverse=True) – gavin Mar 30 '17 at 05:14
  • Not including the code to show an output, the above code will give these results "Sorted by Places visited descending order (most places visited) Sandra is in America. Has been there for 21 days, and has visited 12 places. Mary is in Spain. Has been there for 14 days, and has visited 7 places. Frank is in Holland. Has been there for 52 days, and has visited 6 places. John is in France. Has been there for 23 days, and has visited 13 places." – gavin Mar 30 '17 at 05:15

0 Answers0