Python: how to order a list based on another list

I want to order list1 based on the lines in list2. Any items in list1 that do not match list2 must be placed at the end of a properly ordered list1.

For instance:

list1 = ['Title1-Apples', 'Title1-Oranges', 'Title1-Pear', 'Title1-Bananas']
list2 = ['Bananas', 'Oranges', 'Pear']

list1_reordered_correctly= ['Title1-Bananas','Title1-Oranges','Title1-Pear','Title1-Apples']
+4
source share
5 answers

Here is an idea:

>>> def keyfun(word, wordorder):
...     try:
...         return wordorder.index(word)
...     except ValueError:
...         return len(wordorder)
... 
>>> sorted(list1, key=lambda x: keyfun(x.split('-')[1], list2))
['Title1-Bananas', 'Title1-Oranges', 'Title1-Pear', 'Title1-Apples']

To make it tidier and more efficient ( indexmust cross the list to find the item you need), consider defining your dictionary as a dictionary, i.e.:

>>> wordorder = dict(zip(list2, range(len(list2))))
>>> wordorder
{'Pear': 2, 'Bananas': 0, 'Oranges': 1}
>>> sorted(list1, key=lambda x: wordorder.get(x.split('-')[1], len(wordorder)))
['Title1-Bananas', 'Title1-Oranges', 'Title1-Pear', 'Title1-Apples']
+4
source

This answer is more conceptual than effective.

st1dict = dict((t.split('-')[1],t) for t in st1) #keys->titles
list2titles = list(st1dict[k] for k in list2)    #ordered titles
extras = list(t for t in st1 if t not in list2titles)  #extra titles
print(list2titles+extras)  #the desired answer
0
source

.

sorted_list = sorted(list1, key=lambda x: list2.index(x.split('-')[1]) if x.split('-')[1] in list2 else len(list2) + 1)
0

:

list1 = ['Title1-Apples', 'Title1-Oranges', 'Title1-Pear', 'Title1-Bananas']
list2 = ['Bananas', 'Pear']

# note: converting to set would improve performance of further look up
list2 = set(list2)

def convert_key(item):
    return int(not item.split('-')[1] in list2)



print sorted(list1, key=convert_key)
#  ['Title1-Pear', 'Title1-Bananas', 'Title1-Apples', 'Title1-Oranges']
0

- .

l = ['Title1-Apples', 'Title1-Oranges', 'Title1-Pear', 'Title1-Bananas']
l2 = ['Bananas', 'Oranges', 'Pear']
l3 = []
for elem_sub in l2:
    for elem_super in l:
        if elem_sub in elem_super:
            l3.append(elem_super)

print(l3 + list(set(l)-set(l3)))
0

Source: https://habr.com/ru/post/1628493/


All Articles