Python-Ref > Data structures > List > Advanced list sorting
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Advanced list sorting

In case of sequences (lists, string or dictionaries) it is often needed to sort them in a different way than the default sorting method does. One might for instance want to sort lists according to their last item, or string according to the firts uppercase letter etc.
The sorting mechanism for sequence types described in Sorting list items may be used to perform such complicated sorting of items. The simplest and most efficient approach to this problem is to create a tuple (or a list) that contains the part of the sequence that should be used for sorting and then the whole sequence. After sorting a list of such intermediate tuples, the sequences are again extracted from it.
Expand/Shrink
Zdroj: (list6-0.py)
  1   # sorting according to the second letter
  2   names = ["Jack", "John", "David", "Pete", "Jane"]
  3   
  4   print names  # this is the starting list
  5   
  6   # prepare a list of tuples for sorting
  7   to_sort = []
  8   for name in names:
  9     to_sort.append((name[1], name))
 10   print to_sort  # the prepared list of tuples
 11   to_sort.sort() # sort the list
 12   print to_sort  # the sorted list
 13   
 14   # retrive the original data from the list
 15   sorted = []
 16   for tup in to_sort:
 17     sorted.append( tup[1])
 18   # the sorted list
 19   print sorted
stdout:
['Jack', 'John', 'David', 'Pete', 'Jane']
[('a', 'Jack'), ('o', 'John'), ('a', 'David'), ('e', 'Pete'), ('a', 'Jane')]
[('a', 'David'), ('a', 'Jack'), ('a', 'Jane'), ('e', 'Pete'), ('o', 'John')]
['David', 'Jack', 'Jane', 'Pete', 'John']
Doba běhu: 21.2 ms
The following code uses list comprehensions to simplify the code above.
Expand/Shrink
Zdroj: (list6-1.py)
  1   # sorting according to the second letter
  2   names = ["Jack", "John", "David", "Pete", "Jane"]
  3   
  4   to_sort = [(name[1], name) for name in names]
  5   to_sort.sort()
  6   sorted = [n[1] for n in to_sort]
  7   
  8   print sorted
stdout:
['David', 'Jack', 'Jane', 'Pete', 'John']
Doba běhu: 21.0 ms
In some cases, more convenient approach to the problem of sorting list items according to non-standard criteria could be used. The method sort takes an optional keyword argument key that is a function. In case such a function is provided, the sorting is done according to the result of applying it to individual items and not the content of the items themselves.
Expand/Shrink
Zdroj: (list6-3.py)
  1   # sorting according to the word length
  2   names = [ "John", "Wendy", "Pete", "Jane", "David", "Amanda", "Ix"]
  3   
  4   names.sort( key=len)  # we use the built-in function len
  5   print names
stdout:
['Ix', 'John', 'Pete', 'Jane', 'Wendy', 'David', 'Amanda']
Doba běhu: 21.1 ms
However if you compare the result of the previous example with the next one (using the more general approach), you will find that the latter case has the benefit (for free) of sorting also according to alphabetical order inside the individual length categories.
Expand/Shrink
Zdroj: (list6-4.py)
  1   # sorting according to the word length
  2   names = [ "John", "Wendy", "Pete", "Jane", "David", "Amanda", "Ix"]
  3   
  4   to_sort = [(len( name), name) for name in names]
  5   to_sort.sort()
  6   print [n[1] for n in to_sort]
stdout:
['Ix', 'Jane', 'John', 'Pete', 'David', 'Wendy', 'Amanda']
Doba běhu: 21.1 ms
The following example shows a more advanced code that can be used to sort a list of lists (a matrix) accoding to one specific row.
Expand/Shrink
Zdroj: (list6-2.py)
  1   def sort_table_according_to_row( items, row):
  2       to_sort = []
  3       for item in items:
  4           to_sort.append( (item[row], item))
  5       to_sort.sort()
  6       to_return = []
  7       for item in to_sort:
  8           to_return.append( item[1])
  9       return to_return
 10   
 11   data = [["John", 10, 30, 0.221],
 12           ["Amy",  14, 67, 0.543],
 13           ["David", 7, 97, 0.111],
 14           ["Mike",  3, 55, 0.501]]
 15   
 16   for i in range( 4):
 17       print sort_table_according_to_row( data, i)
stdout:
[['Amy', 14, 67, 0.54300000000000004], ['David', 7, 97, 0.111], ['John', 10, 30, 0.221], ['Mike', 3, 55, 0.501]]
[['Mike', 3, 55, 0.501], ['David', 7, 97, 0.111], ['John', 10, 30, 0.221], ['Amy', 14, 67, 0.54300000000000004]]
[['John', 10, 30, 0.221], ['Mike', 3, 55, 0.501], ['Amy', 14, 67, 0.54300000000000004], ['David', 7, 97, 0.111]]
[['David', 7, 97, 0.111], ['John', 10, 30, 0.221], ['Mike', 3, 55, 0.501], ['Amy', 14, 67, 0.54300000000000004]]
Doba běhu: 21.2 ms