在Python中的二维数组中查找公共元素和元素的邻居

Finding the common element and the neighbor of the element in a two-dimensional array in Python

list = [[2,4], [4,3], [7,6], [8,2], [9,11], [10,11], [13,18], [10,23]]

我有一个二维数组。如果这些嵌套数组中有任何公共元素,我想将公共元素和它旁边的元素包含在一个新数组中。我在上面给出了一个示例数组。 我想将公共元素和邻居添加到如下数组中。

list_dup_out = [[2,3,4,8], [9,10,11,23]]

你能分享一个示例 python 代码吗?

我会保留所有关联的字典,然后将具有多个关联的项目转储到一个集合中以对它们进行重复数据删除:

>>> arr = [[2,4], [4,3], [7,6], [8,2], [9,11]]
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for x in arr:
...     for y in x:
...         for z in x:
...             if y != z:
...                 d[y].append(z)
... 
>>> sorted({y for x in d.values() for y in x if len(x) > 1})
[2, 3, 4, 8]