944,150 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 1059
  • Python RSS
1

Sorting complicated objects (Python)

by on Nov 8th, 2009
This code snippet shows you how to sort more complicated objects like a list of lists (or tuples) by selecting an index of the item to sort by.
Python Code Snippet (Toggle Plain Text)
  1. # sorting a list of lists/tuples by index
  2. # tested with Python 2.5.4 and Python 3.1.1 by vegaseat
  3.  
  4. import operator
  5.  
  6. # in [[name1, score1], [name2, score2], ...] score is at index=1
  7. score = operator.itemgetter(1)
  8.  
  9. player_score = [['zoro', 88], ['jerry', 68], ['albert', 99]]
  10.  
  11. # sort by decreasing score
  12. player_score.sort(key=score, reverse=True)
  13.  
  14. print(player_score)
  15.  
  16. """my result -->
  17. [['albert', 99], ['zoro', 88], ['jerry', 68]]
  18. """
  19.  
  20. print('-'*50)
  21.  
  22. # sort a more complicated combination of lists/tuples:
  23. mylist = [
  24. (1, ['a', '3.1', 'ad']),
  25. (2, ['b', '4.0', 'bd']),
  26. (3, ['c', '2.5', 'cd']),
  27. ]
  28. # sort by item at index [1][1] of each tuple in mylist
  29. # using a helper function like lambda
  30. newlist = sorted(mylist, key=lambda tup: tup[1][1])
  31.  
  32. print(newlist)
  33.  
  34. """my result (made pretty) -->
  35. [
  36. (3, ['c', '2.5', 'cd']),
  37. (1, ['a', '3.1', 'ad']),
  38. (2, ['b', '4.0', 'bd'])
  39. ]
  40. """
  41.  
  42. print('-'*50)
  43.  
  44. # or ...
  45. # sort by item at index [1][1] of each tuple in mylist
  46. # use the Schwartzian transform algorithm
  47. # temporarily put a copy of indexed item in front
  48. templist = [(x[1][1], x) for x in mylist]
  49. templist.sort()
  50. # remove temporary front item after sorting
  51. newlist = [val for (temp, val) in templist]
  52.  
  53. print(newlist)
  54.  
  55. """my result (made pretty) -->
  56. [
  57. (3, ['c', '2.5', 'cd']),
  58. (1, ['a', '3.1', 'ad']),
  59. (2, ['b', '4.0', 'bd'])
  60. ]
  61. """
Comments on this Code Snippet
Nov 24th, 2009
0

Re: Sorting complicated objects (Python)

Here is the code to sort a dictionary of dictionaries:
Python Syntax (Toggle Plain Text)
  1. # sorted display of a dictionary of dictionaries
  2.  
  3. def dict_of_dict_sort(dd, key):
  4. """
  5. print out selected items from a dictionary of dictionaries dd
  6. sorted by a given key
  7. """
  8. for k in sorted(dd, key=lambda x: dd[x][key]):
  9. print( k, dd[k]['age'], dd[k]['country'] )
  10.  
  11.  
  12. user_dict = {
  13. 'Bill': {'age': 39, 'country': 'USA'},
  14. 'Dave' : {'age': 26, 'country': 'Canada'},
  15. 'Olaf' : {'age': 33, 'country': 'Sweden'}
  16. }
  17.  
  18. # display sorted by age
  19. dict_of_dict_sort(user_dict, 'age')
  20.  
  21. """my result -->
  22. Dave 26 Canada
  23. Olaf 33 Sweden
  24. Bill 39 USA
  25. """
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Message:
Previous Thread in Python Forum Timeline: Displaying eyes
Next Thread in Python Forum Timeline: Why does the interpreter expect an owned reference?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC