如何基于python中的常见项目对列表项进行分组

I have below list which contains tuples:

mylist = [(0, 2), (1, 2), (1, 3)] 

As we can see from above list, item 0 (0, 2) and item 1 (1, 2) has common number 2 and item 1 (1, 2) and item 2 (1, 3) has common number 1, so how can I create a new list or reformat the existing list such that it looks like below:

my_new_list = [(0, 1, 2), (1, 2, 3)]

mylist can have n number of items (tuples) in it.

评论