summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraric <none@none>2006-11-10 16:49:35 +0000
committeraric <none@none>2006-11-10 16:49:35 +0000
commitbb5272bfa43bf94d058626f8533717f64c7d3081 (patch)
treec96a6ef3aad3e098764a664164a129363850d70b
parent615d65ad6b1246ca665e310ba9843c313eafb0ea (diff)
downloadnetworkx-bb5272bfa43bf94d058626f8533717f64c7d3081.tar.gz
optional edge_data in to_dict_of_dicts to allow arbitrary
specification dictionary value (e.g. use edge_data=1 to make an adjacency matrix type representation). --HG-- extra : convert_revision : svn%3A3ed01bd8-26fb-0310-9e4c-ca1a4053419f/networkx/trunk%40447
-rw-r--r--networkx/convert.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/networkx/convert.py b/networkx/convert.py
index fdece992..158a1a0a 100644
--- a/networkx/convert.py
+++ b/networkx/convert.py
@@ -152,24 +152,28 @@ def from_dict_of_lists(d,create_using=None):
return G
-def to_dict_of_dicts(G,nodelist=None,weighted=False):
+def to_dict_of_dicts(G,nodelist=None,edge_data=None):
"""Return graph G as a Python dictionary of dictionaries.
If nodelist is defined return a dict of dicts with only those nodes.
+
+ If edge_data is given, the value of the dictionary will be
+ set to edge_data for all edges. This is useful to make
+ an adjacency matrix type representation with 1 as the edge data.
"""
if nodelist is None:
nodelist=G.nodes()
- if not weighted or not hasattr(G,"get_edge"):
- w=lambda x,y:1
+ if edge_data is not None:
+ get_edge=lambda x,y:edge_data
else:
- w=lambda x,y:G.get_edge(x,y)
+ get_edge=G.get_edge
d = {}
for u in nodelist:
d[u]={}
for v in G.neighbors(u):
- d[u][v]=w(u,v)
+ d[u][v]=get_edge(u,v)
return d