summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjeff <none@none>2006-11-10 08:35:30 +0000
committerjeff <none@none>2006-11-10 08:35:30 +0000
commit0698b656a3054f2219062f8089729b96209eff10 (patch)
tree40a57e7eddb94a8010a54c81a5bab41ba96f7ec8
parentf019fb6b50c3b70d5df5a58c306074d0d50b2359 (diff)
downloadnetworkx-0698b656a3054f2219062f8089729b96209eff10.tar.gz
to_dict_of_dicts() probably should assign values of 1 rather than None
if the graph is unweighted. In addition, add flag to request weights or not. (I.e., do we want the adjacency matrix or the weight matrix in sparse form.) Default is the old behavior of providing the weight matrix. --HG-- extra : convert_revision : svn%3A3ed01bd8-26fb-0310-9e4c-ca1a4053419f/networkx/trunk%40442
-rw-r--r--networkx/convert.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/networkx/convert.py b/networkx/convert.py
index 70282dde..52684115 100644
--- a/networkx/convert.py
+++ b/networkx/convert.py
@@ -152,21 +152,24 @@ def from_dict_of_lists(d,create_using=None):
return G
-def to_dict_of_dicts(G,nodelist=None):
+def to_dict_of_dicts(G,nodelist=None,weighted=False):
"""Return graph G as a Python dictionary of dictionaries.
If nodelist is defined return a dict of dicts with only those nodes.
-
+
"""
if nodelist is None:
nodelist=G.nodes()
+ if not weighted or not hasattr(G,"get_edge"):
+ w=lambda x,y:1
+ else:
+ w=lambda x,y:G.get_edge(x,y)
d = {}
for u in nodelist:
d[u]={}
for v in G.neighbors(u):
- data=G.get_edge(u,v)
- d[u][v]=data
+ d[u][v]=w(u,v)
return d