2

I have two Python lists as below:

 list1 = ['River (OTH)', 'River (N)', 'River']
 list2 = ['River (OTH) sales', 'River (N) sales', 'River sales']

And I sorted the above list with the code:

list1.sort()   
list2.sort()

After sorting I get the list as:

list1 = ['River', 'River (N)', 'River (OTH)']
list2 = ['River (OTH) sales', 'River (N) sales', 'River sales'] 

I need the list2 also to be sorted like:

list2 = ['River sales', 'River (N) sales', 'River (OTH) sales']

How can I achieve this?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Applying `sort` on the second list do not provide the output you wrote but : `['River (N) sales', 'River (OTH) sales', 'River sales']`. If you are looking to reversed order, call `sorted(list, reverse=True)` – Alexandre B. Jul 01 '19 at 10:27

1 Answers1

2
  1. This is assuming both lists are originally co-ordered.

You could use the function zip in order to "glue" the two lists together, than sort this new list according to the elements of list1, and then separate them back. More easy to show:

list1=['River (OTH)','River (N)','River']
list2=['River (OTH) sales','River (N) sales','River sales']

zip_list = list(zip(list1, list2))
#  zip_list = [('River (OTH)', 'River (OTH) sales'), ('River (N)', 'River (N) sales'), ('River', 'River sales')]

zip_list.sort(key=lambda elem: elem[0])
#  zip_list = [('River', 'River sales'), ('River (N)', 'River (N) sales'), ('River (OTH)', 'River (OTH) sales')]

sorted_list1 = [elem[0] for elem in zip_list]
#  sorted_list1 = ['River', 'River (N)', 'River (OTH)']

sorted_list2 = [elem[1] for elem in zip_list]
#  sorted_list2 = ['River sales', 'River (N) sales', 'River (OTH) sales']
  • Note that you can get the separated lists back more efficiently according to this answer. (sorted_list1, sorted_list2 = [list(t) for t in zip(*zip_list)])

In case you want it reversed, simply add reverse=True to the sort() call.

Another interesting solution could be using enumerate to "remember" the changes occured by indices:

indexed_list1 = list(enumerate(list1))
#  [(0, 'River (OTH)'), (1, 'River (N)'), (2, 'River')]

indexed_list1.sort(key=lambda x: x[1])
#  [(2, 'River'), (1, 'River (N)'), (0, 'River (OTH)')]

indices, sorted_list1 = (list(l) for l in zip(*indexed_list1))
#  sorted_list1 = ['River', 'River (N)', 'River (OTH)']
#  indices = [2, 1, 0]

sorted_list2 = [list2[i] for i in indices]
#  ['River sales', 'River (N) sales', 'River (OTH) sales']

  1. If the lists are not co-ordered, but the only distinction is the sales word (or any other word for that matter) in the end, you could simply do:
import re

list2.sort(key=lambda s: re.sub("\s*sales$", "", s))
# list2 = ['River sales', 'River (N) sales', 'River (OTH) sales']

This will sort the list while ignoring the ending word sales. I used regex to allow any number of spaces. The use of rstrip might change other parts of the string.


  1. Otherwise, this is too specific and requires a specific solution
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61