merge lists with intersection

前端 未结 3 2154
南笙
南笙 2020-12-18 02:38

Given that:

g=[[], [], [0, 2], [1, 5], [0, 2, 3, 7], [4, 6], [1, 4, 5, 6], [], [], [3, 7]]

How can I compare each list within g so that for

3条回答
  •  无人及你
    2020-12-18 02:47

    Here's a quickie that will give a list of all the sets that intersect:

    sets = [set(i+j) for i in g for j in g if i!=j and (set(i) & set(j))]
    

    Note that each result will be repeated, since each list is being compared twice, once on the left and once on the right.

提交回复
热议问题