@@ -1920,6 +1920,7 @@ def from_networkx(cls, g):
1920
1920
@param g: networkx Graph or DiGraph
1921
1921
"""
1922
1922
import networkx as nx
1923
+ from collections import defaultdict
1923
1924
1924
1925
# Graph attributes
1925
1926
gattr = dict (g .graph )
@@ -1942,13 +1943,15 @@ def from_networkx(cls, g):
1942
1943
graph .vs [vd [v ]][key ] = val
1943
1944
1944
1945
# Edges and edge attributes
1945
- # NOTE: we need to do both together to deal well with multigraphs
1946
- # Each e might have a length of 2 (graphs) or 3 (multigraphs, the
1947
- # third element is the "color" of the edge)
1948
- for e , (_ , _ , datum ) in zip (g .edges , g .edges .data ()):
1949
- eid = graph .add_edge (vd [e [0 ]], vd [e [1 ]])
1950
- for key , val in list (datum .items ()):
1951
- eid [key ] = val
1946
+ eattr_names = {name for (_ , _ , data ) in g .edges .data () for name in data }
1947
+ eattr = defaultdict (list )
1948
+ edges = []
1949
+ for (u , v , data ) in g .edges .data ():
1950
+ edges .append ((vd [u ], vd [v ]))
1951
+ for name in eattr_names :
1952
+ eattr [name ].append (data .get (name ))
1953
+
1954
+ graph .add_edges (edges , eattr )
1952
1955
1953
1956
return graph
1954
1957
0 commit comments