Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two lists of strings

Given two lists of strings that contain duplicates save for one element in each list, how would you combine the two into a single list that contains one copy of every value in list order?

For example, given the following two lists in Python:

a = ['Second', 'Third', 'Fourth']
b = ['First', 'Second', 'Third']

Or

a = ['First', 'Third', 'Fourth']
b = ['First', 'Second', 'Third']

How would you combine the two lists to get a single list like this:

result = ['First', 'Second', 'Third', 'Fourth']

Note that the exact values of the strings cannot necessarily be trusted to help with ordering the elements.

I am aware of the possibility that there will be some cases with no definitive way to lock the list down to a particular order, and will probably have to special-case those, but for the general cases I'd rather have a procedure to follow. For example:

a = ['First', 'Third', 'Fourth']
b = ['First', 'Second', 'Fourth']

This could have 'Third' and 'Second' in either order, as there's no item on both lists between them to provide a guideline.

Edit: I should explain the strings a bit further, as I see many of you are assuming that I can merely sort a raw merge of the two lists, and this just isn't going to work.

I'm taking story titles, which, for each story, only list the other instalments and not the linked story itself. So by taking two lists (or possibly more, I'm not sure), I can come up with a full list of the instalments to put them in their proper order.

like image 904
Raceimaztion Avatar asked Dec 16 '22 03:12

Raceimaztion


2 Answers

Simple algorythm:

  1. Concat lists
  2. Remove dups
  3. Sort

Code:

def order_list(lst, order_dict):
     return sorted(list(lst), key = lambda x: order_dict.get(x, -1))

c = list(set(a + b))
ord_dict = {"First": 1, "Second": 2, "Third": 3, "Fourth": 4}
order_list(c, ord_dict)
like image 180
Lukasz Madon Avatar answered Dec 26 '22 11:12

Lukasz Madon


You have 2 different concerns here:

  • Duplicate elimination
  • Ordering

I would do them separately. Duplication elimination is simple enough. Use a set:

>>> a = ['Second', 'Third', 'Fourth']
>>> b = ['First', 'Second', 'Third']
>>> x = set(a)
>>> x
set(['Second', 'Fourth', 'Third'])
>>> x.update(b)
>>> x
set(['Second', 'Fourth', 'Third', 'First'])

Then you'll need to a define the ordering somehow. The simplest way to do that might be to map each possible element to a value:

>>> order_dict = {'First': 1, 'Second': 2, 'Third': 3, 'Fourth': 4}
>>> result = sorted(list(x), key=lambda i: order_dict[i])
>>> result
['First', 'Second', 'Third', 'Fourth']

Alternatively, you could use some kind of compare function with sorted's cmp argument if you can define one for your values.

Hope this helps.

like image 21
jpmc26 Avatar answered Dec 26 '22 10:12

jpmc26