diff options
author | Jarrod Millman <jarrod.millman@gmail.com> | 2017-08-17 17:15:20 -0700 |
---|---|---|
committer | Jarrod Millman <jarrod.millman@gmail.com> | 2017-08-17 18:13:44 -0700 |
commit | 433a3197814ab7b59d0e1caaa9f535431fd98070 (patch) | |
tree | 09029bee7d98a7fc4242fa004ddf927c501b401f /networkx/relabel.py | |
parent | 90544b4fa5a7397a86e69ebf5778f4a0f6eafe1e (diff) | |
download | networkx-433a3197814ab7b59d0e1caaa9f535431fd98070.tar.gz |
Comply with pep8
Diffstat (limited to 'networkx/relabel.py')
-rw-r--r-- | networkx/relabel.py | 72 |
1 files changed, 35 insertions, 37 deletions
diff --git a/networkx/relabel.py b/networkx/relabel.py index f6aeda6f..c4c2e6bc 100644 --- a/networkx/relabel.py +++ b/networkx/relabel.py @@ -1,13 +1,11 @@ -# Copyright (C) 2006-2013 by +# Copyright (C) 2006-2017 by # Aric Hagberg <hagberg@lanl.gov> # Dan Schult <dschult@colgate.edu> # Pieter Swart <swart@lanl.gov> # All rights reserved. # BSD license. import networkx as nx -__author__ = """\n""".join(['Aric Hagberg <aric.hagberg@gmail.com>', - 'Pieter Swart (swart@lanl.gov)', - 'Dan Schult (dschult@colgate.edu)']) + __all__ = ['convert_node_labels_to_integers', 'relabel_nodes'] @@ -29,48 +27,48 @@ def relabel_nodes(G, mapping, copy=True): Examples -------- To create a new graph with nodes relabeled according to a given - dictionary:: + dictionary: - >>> G = nx.path_graph(3) - >>> sorted(G) - [0, 1, 2] - >>> mapping = {0: 'a', 1: 'b', 2: 'c'} - >>> H = nx.relabel_nodes(G, mapping) - >>> sorted(H) - ['a', 'b', 'c'] + >>> G = nx.path_graph(3) + >>> sorted(G) + [0, 1, 2] + >>> mapping = {0: 'a', 1: 'b', 2: 'c'} + >>> H = nx.relabel_nodes(G, mapping) + >>> sorted(H) + ['a', 'b', 'c'] Nodes can be relabeled with any hashable object, including numbers - and strings:: - - >>> import string - >>> G = nx.path_graph(26) # nodes are integers 0 through 25 - >>> sorted(G)[:3] - [0, 1, 2] - >>> mapping = dict(zip(G, string.ascii_lowercase)) - >>> G = nx.relabel_nodes(G, mapping) # nodes are characters a through z - >>> sorted(G)[:3] - ['a', 'b', 'c'] - >>> mapping = dict(zip(G, range(1, 27))) - >>> G = nx.relabel_nodes(G, mapping) # nodes are integers 1 through 26 - >>> sorted(G)[:3] - [1, 2, 3] + and strings: + + >>> import string + >>> G = nx.path_graph(26) # nodes are integers 0 through 25 + >>> sorted(G)[:3] + [0, 1, 2] + >>> mapping = dict(zip(G, string.ascii_lowercase)) + >>> G = nx.relabel_nodes(G, mapping) # nodes are characters a through z + >>> sorted(G)[:3] + ['a', 'b', 'c'] + >>> mapping = dict(zip(G, range(1, 27))) + >>> G = nx.relabel_nodes(G, mapping) # nodes are integers 1 through 26 + >>> sorted(G)[:3] + [1, 2, 3] To perform a partial in-place relabeling, provide a dictionary mapping only a subset of the nodes, and set the `copy` keyword - argument to False:: + argument to False: - >>> 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'] + >>> 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'] - A mapping can also be given as a function:: + A mapping can also be given as a function: - >>> G = nx.path_graph(3) - >>> H = nx.relabel_nodes(G, lambda x: x ** 2) - >>> list(H) - [0, 1, 4] + >>> G = nx.path_graph(3) + >>> H = nx.relabel_nodes(G, lambda x: x ** 2) + >>> list(H) + [0, 1, 4] Notes ----- |