1

I have an input in a nested list like:

input = [ [1,2],[1,3],[1,5],[2,3],[2,5],[3,1],[3,3],[4,6] ]

And I want to merge the same element together as output like:

output = [ [1,2,3,5], [4,6] ]

So far, I'd tried with defaultdict , unique and set().intersection to generate the result, but not as expected.

Is there any easier way to accomplish this? I'll be appreciated with any idea can help.

Thanks!

karen wu
  • 15
  • 1
  • 6
  • What's the logic behind the output? – Eric Duminil May 17 '17 at 13:11
  • It is the *merge if there are common elements* logic. A very similar question was posted yesterday about that. – Ma0 May 17 '17 at 13:11
  • 2
    Let's see the code where you used defaultdict, unique and set. It will make a useful starting point. – Kevin May 17 '17 at 13:12
  • If you need help debugging your approach: please include your code in the question ([edit]). If you're just interested in a working way I think I found a suitable duplicate. :) – MSeifert May 17 '17 at 13:13
  • Thank you all! I think I should check the post first which I didnt found. – karen wu May 17 '17 at 14:26

0 Answers0