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.
Simple algorythm:
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)
You have 2 different concerns here:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With