diff options
author | Jeffrey Finkelstein <jeffrey.finkelstein@gmail.com> | 2015-05-20 22:43:28 -0400 |
---|---|---|
committer | Mridul Seth <seth.mridul@gmail.com> | 2015-06-11 23:55:58 +0530 |
commit | 203295422d6f57ed4cb4c523057a4fa53e4485fc (patch) | |
tree | 821e591f3701106f5256775071a33d0021a6e29e /networkx/relabel.py | |
parent | d49733b61fb7dedfbbbdd9c04464adf6beb5db07 (diff) | |
download | networkx-203295422d6f57ed4cb4c523057a4fa53e4485fc.tar.gz |
Makes Graph.nodes() return iterator instead of list
Previously `Graph.nodes()` returned a list of nodes and
`Graph.nodes_iter()` returned an iterator over nodes. With this commit,
the former function now returns an iterator and the latter no longer
exists.
Diffstat (limited to 'networkx/relabel.py')
-rw-r--r-- | networkx/relabel.py | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/networkx/relabel.py b/networkx/relabel.py index d00ba3a7..c188cc1c 100644 --- a/networkx/relabel.py +++ b/networkx/relabel.py @@ -41,20 +41,17 @@ def relabel_nodes(G, mapping, copy=True): Partial in-place mapping: - >>> G=nx.path_graph(3) # nodes 0-1-2 - >>> mapping={0:'a',1:'b'} # 0->'a' and 1->'b' - >>> G=nx.relabel_nodes(G,mapping, copy=False) - - print(G.nodes()) - [2, 'b', 'a'] + >>> G = nx.path_graph(3) # nodes 0-1-2 + >>> mapping = {0: 'a', 1: 'b'} # 0->'a' and 1->'b' + >>> G = nx.relabel_nodes(G, mapping, copy=False) + >>> sorted(G, key=str) + [2, 'a', 'b'] Mapping as function: - >>> G=nx.path_graph(3) - >>> def mapping(x): - ... return x**2 - >>> H=nx.relabel_nodes(G,mapping) - >>> print(H.nodes()) + >>> G = nx.path_graph(3) + >>> H = nx.relabel_nodes(G, lambda x: x ** 2) + >>> list(H) [0, 1, 4] Notes @@ -184,8 +181,7 @@ def convert_node_labels_to_integers(G, first_label=0, ordering="default", if ordering == "default": mapping = dict(zip(G.nodes(), range(first_label, N))) elif ordering == "sorted": - nlist = G.nodes() - nlist.sort() + nlist = sorted(G.nodes()) mapping = dict(zip(nlist, range(first_label, N))) elif ordering == "increasing degree": dv_pairs = [(d,n) for (n,d) in G.degree_iter()] |