Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表与公共元素组合_Python_List_Networkx - Fatal编程技术网

Python 将列表与公共元素组合

Python 将列表与公共元素组合,python,list,networkx,Python,List,Networkx,例如,假设我有以下嵌套列表: L = [['John','Sayyed'], ['John' , 'Simon'] ,['bush','trump'], ['Sam','Suri','NewYork'],['Suri','Orlando','Canada']] 如何通过将具有公共元素的子列表与组中至少另一个子列表的并集来对这些子列表进行分组?因此,对于上一个示例,结果应该是: [['John','Sayyed','Simon'] ,['bush','trump'], ['Sam',

例如,假设我有以下嵌套列表:

L = [['John','Sayyed'], ['John' , 'Simon'] ,['bush','trump'],
     ['Sam','Suri','NewYork'],['Suri','Orlando','Canada']]
如何通过将具有公共元素的子列表与组中至少另一个子列表的并集来对这些子列表进行分组?因此,对于上一个示例,结果应该是:

[['John','Sayyed','Simon'] ,['bush','trump'],
 ['Sam','Suri','NewYork','Orlando','Canada']]
因此,前两个子列表在共享
'John'
时被合并。
有人能分享他们的宝贵想法吗?

在许多情况下,将问题建模为一个图形,可以使相当复杂的任务变得更容易。在这种情况下,从图论的角度来看,我们要寻找的是图的形状

因此,一个简单的方法是使用生成一个图,并使用添加列表作为图边。然后使用,它将精确地为您提供图形中连接组件集的列表:

import networkx as nx 

L = [['John','Sayyed'], ['John' , 'Simon'] ,['bush','trump']]

G=nx.Graph()
G.add_edges_from(L)
list(nx.connected_components(G))

[{'John', 'Sayyed', 'Simon'}, {'bush', 'trump'}]

有多个(>2)项的子列表如何? 如果子列表包含超过2个
2
元素,则可以使用
nx.add_path
将它们添加为路径,而不是节点,因为它们可以连接多个节点:

L = [['John','Sayyed'], ['John' , 'Simon'] ,['bush','trump'],
     ['Sam','Suri','NewYork'],['Suri','Orlando','Canada']]

G=nx.Graph()
for l in L:
    nx.add_path(G, l)
list(nx.connected_components(G))

[{'John', 'Sayyed', 'Simon'},
 {'bush', 'trump'},
 {'Canada', 'NewYork', 'Orlando', 'Sam', 'Suri'}]
我们还可以通过以下方式使这些连接的组件栩栩如生:


关于连通分量(图论) 更详细的解释如下:

在图论中,无向图的连通分量(或仅连通分量)是一个子图,其中任意两个顶点通过路径相互连接,并且不与超图中的其他顶点连接

因此,本质上,该代码创建了一个图,该图具有列表中的边,其中每条边由两个值组成,
u,v
,其中
u
v
将是由该边连接的节点

因此,子列表与至少一个具有公共元素的子列表的并集可以转化为一个图论问题,因为所有节点都可以通过现有路径相互访问。

如果顺序很重要且列表很大,则可以使用这种双管齐下的方法:

 l = [['john', 'sayyid'], ['john', 'simon'], ['b', 't']]

 def join(l1, l2):
     mset = set(l1)
     result = l1[:] # deep copy
     for each in l2:
         if each in mset:
             continue
         else:
             result.append(each)
     return result
要在主列表中合并,只需按其排名调用列表并弹出原始列表:

l1 = l.pop(0)
l2 = l.pop(0)
l.insert(0, join(l1, l2))
>>> l:
[['john', 'sayyid', 'simon'], ['b', 't']]
要合并两个列表,请执行以下操作:

merge = lambda l1, l2: l1 + [ x for x in l2 if x not in l1 ]
为了提高效率,在
l1
上创建一个
集合

一个简单的方法

L = [['John','Sayyed'], [ 'John' , 'Simon'] ,['bush','trump']]
L[0].extend([x for x in L[1] if x not in L[0]])
L.pop(1)
print(L) 


您可以在
networkx
中使用功能
连接的\u组件

import networkx as nx 
​
L = [['John','Sayyed'], ['John' , 'Simon'] ,['bush','trump'],
     ['Sam','Suri','NewYork'],['Suri','Orlando','Canada']]
​
G = nx.Graph()
​
for i in L:
    G.add_path(i)
​
lst = list(nx.connected_components(G))
print(lst)
输出:

[{'John', 'Sayyed', 'Simon'},
 {'bush', 'trump'},
 {'Canada', 'NewYork', 'Orlando', 'Sam', 'Suri'}]

有趣的方法,请解释一下,如果子列表包含两个以上的元素,会发生什么?@Aiyaz更新了一个更通用的情况,即子列表包含两个以上的元素
[{'John', 'Sayyed', 'Simon'},
 {'bush', 'trump'},
 {'Canada', 'NewYork', 'Orlando', 'Sam', 'Suri'}]