0

I am trying to get rid of all duplicates in res, but this is what happened. Res still have duplicates such as [1, 2], [1, 3],[2, 3]. How can I get res [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]?

res=[[], [1], [2], [3], [1, 2], [1, 3], [1, 2], [2, 3], [1, 3], [2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]

res = [i for i, _ in itertools.groupby(res)]
daleng
  • 11
  • 2
  • this is what i got res=[[], [1], [2], [3], [1, 2], [1, 3], [1, 2], [2, 3], [1, 3], [2, 3], [1, 2, 3]] – daleng Oct 17 '22 at 01:12

1 Answers1

0

You could try this instead: Remember that the groupby only works if the items are in the sorted order, which in the input is not the case.

How it works: it use the same key to group similar items (lists). you could try it in the for-loop to see the intermediate results.


from itertools import groupby
>>>res2 = sorted(res)

>>>ans = [i for i, _ in groupby(res2) ]
>>>ans
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]

try for-loop to understand it:

for k, g in groupby(res2):
    print(k, list(g))

    
[] [[]]
[1] [[1]]
[1, 2] [[1, 2], [1, 2]]
[1, 2, 3] [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
[1, 3] [[1, 3], [1, 3]]
[2] [[2]]
[2, 3] [[2, 3], [2, 3]]
[3] [[3]]
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23