我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用networkx.to_edgelist()。
def convert_network_3Dto2D(network): """ Reshape a 3D grid graph into a 2D one, preserving adjacency and edge&node attributes. """ assert network.graph.has_key("grid3D_dimensions") M,N,K = network.graph["grid3D_dimensions"] reshape = lambda node: (node[0],node[1] + node[2]*N) nodes_2D = [(reshape(node_attr[0]),node_attr[1]) for node_attr in network.node.items()] edges_3D = nx.to_edgelist(network) edges_2D = [(reshape(e[0]),reshape(e[1]),e[2]) for e in edges_3D] g = nx.from_edgelist(edges_2D) g.add_nodes_from(nodes_2D) g.graph["grid_dimensions"] = (M,N*K) g.graph["original_grid3D_dimensions"] = (M,N,K) return g
def save_as_sgraph(self, graph_path): """ Saves the graph as sgraph Parameters ---------- graph_path: string The graph path. Examples -------- >>> g.save_as_sgraph("graph.sgraph") """ edges = nx.to_edgelist(self._graph) res = {'source': [], 'dest': [], "attr": []} for edge in edges: res['source'].append(edge[0]) res['dest'].append(edge[1]) res['attr'].append(edge[2]) save_nx_as_sgraph(res, graph_path)
def networkx_to_igraph(G): mapping = dict(zip(G.nodes(),range(G.number_of_nodes()))) reverse_mapping = dict(zip(range(G.number_of_nodes()),G.nodes())) G = nx.relabel_nodes(G,mapping) G_ig = ig.Graph(len(G), list(zip(*list(zip(*nx.to_edgelist(G)))[:2]))) return G_ig, reverse_mapping
def __init__(self, graph, inputc=None, inputc_inh=None, **par): self.inputc = inputc.copy(order='C') if inputc is not None else None self.inputc_inh = inputc_inh.copy(order='C') if inputc_inh is not None else None self.edges = [str(e) for e in nx.to_edgelist(graph)] self.par = par par_s = [str((key, self.par[key])) for key in sorted(self.par.keys())] self.hashable = ''.join(self.edges) self.hashable += ''.join(par_s) self.hashable += str(self.inputc) self.hashable += str(self.inputc_inh)
def export(self): export = nx.to_edgelist(self.transforms) for e in export: e[2]['matrix'] = np.array(e[2]['matrix']).tolist() return export
def networkx_to_igraph(G): mapping = dict(zip(G.nodes(), range(G.number_of_nodes()))) reverse_mapping = dict(zip(range(G.number_of_nodes()), G.nodes())) G = nx.relabel_nodes(G, mapping) G_ig = ig.Graph(len(G), list(zip(*list(zip(*nx.to_edgelist(G)))[:2]))) return G_ig, reverse_mapping