简体   繁体   English

如何从节点列表中获取相应的边

[英]How to get the corresponding edges from a list of nodes

I have a list of nodes in a form similar to this: Nodelist=[[1,2,3],[4,7,6],[7,2,9]] Such that there is an edge that connects them, for example 1 is connected to 2 and 2 to 3. These are not the complete set of nodes in my graph.我有一个类似于以下形式的节点列表: Nodelist=[[1,2,3],[4,7,6],[7,2,9]] 这样就有一条边连接它们,例如 1 连接到 2 和 2 连接到 3。这些不是我图中的完整节点集。

What I want to do is return a list of edges if there is a connection between the two nodes in Nodelist.如果 Nodelist 中的两个节点之间存在连接,我想要做的是返回一个边列表。 ie output=[(1,2),(2,3),(3,1),...]即输出=[(1,2),(2,3),(3,1),...]

What I know already, every node in a sublist of Nodelist is connected, ie there is an edge between (1,2),(2,3),(3,1).我已经知道,Nodelist 的子列表中的每个节点都是连接的,即 (1,2),(2,3),(3,1) 之间有一条边。

I hope what i'm asking makes sense, any help would be appreciated.我希望我问的是有道理的,任何帮助将不胜感激。 Please let me know if I need to clarify on anything.如果我需要澄清任何事情,请告诉我。

Try this:尝试这个:

from itertools import combinations

[list(combinations(x, 2)) for x in node_list] 

combinations gives all combinations of a list of the given size. combinations给出给定大小的列表的所有组合。 So combinations([1,2,3], 2) would give you [(1,2), (2,3), (3,1)] Just do that for each of the node_lists.因此, combinations([1,2,3], 2)会给你[(1,2), (2,3), (3,1)]只需对每个 node_lists 执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM