简体   繁体   中英

Remove duplicates from the list

I have a main List to store different lists that could be added at any time to main List. The problem I have is to delete the same values from the lists in the main list. So for example:

Initial List of lists:

[[('not', ('box', 'p')), ('diamond', 'p'), ('box', 'q'), ('diamond', 'q'),
  ('diamond', 'r')], [('not', 'p'), 'q'], ['p', 'q'], ['q', 'q'],
 ['r', 'q']]

Desired return:

[[('not', ('box', 'p')), ('diamond', 'p'), ('box', 'q'), ('diamond', 'q'),
  ('diamond', 'r')], [('not', 'p'), 'q'], ['p', 'q'], ['q'], ['r', 'q']]

Second example

Initial:

[[('not', ('box', 'p')), ('diamond', 'p'), ('box', 'q'), ('diamond', 'q'),
  ('diamond', 'q')], [('not', 'p'), 'q'], ['p', 'q'], ['q', 'q'],
 [('not', r'), 'q']]

return

[[('not', ('box', 'p')), ('diamond', 'p'), ('box', 'q'), ('diamond', 'q')],
 [('not', 'p'), 'q'], ['p', 'q'], ['q'], [('not', r'), 'q']]

Importantly, order must be the same and only the list inside the main list need not to have duplicates. I have seen many suggestions on stack overflow but none of them work because checking element by element would just leave me with 'diamond' or 'box' values on its own. Where in fact I need ('diamond','q') tuple to be added in full. This question is different to similar questions because I want to sort a single list inside the main list.

from collections import OrderedDict

init_list = [[('not', ('box', 'p')), ('diamond', 'p'), ('box', 'q'), ('diamond', 'q'), ('diamond', 'q')], [('not', 'p'), 'q'], ['p', 'q'], ['q', 'q'], [('not', 'r'), 'q']]

uniq_list = [list(OrderedDict.fromkeys(l)) for l in init_list]

OrderedDict allows you to create an ordered set since OrderedDict.fromkeys(l) returns a dictionary with keys from l preserving their order (and eliminating duplicates). list(OrderedDict) simply returns the dict's keys as a list .

You can use this recipe for an OrderedSet and then

init_list = # your list of lists
uniq_list = [list(OrderedSet(l)) for l in init_list]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM