A NodeView of the Graph as G.nodes or G.nodes(). Can be used as `G.nodes` for data lookup and for set-like operations. Can also be used as `G.nodes(data='color', default=None)` to return a NodeDataView which reports specific node data but no set operations. It presen
(self)
| 755 | |
| 756 | @cached_property |
| 757 | def nodes(self): |
| 758 | """A NodeView of the Graph as G.nodes or G.nodes(). |
| 759 | |
| 760 | Can be used as `G.nodes` for data lookup and for set-like operations. |
| 761 | Can also be used as `G.nodes(data='color', default=None)` to return a |
| 762 | NodeDataView which reports specific node data but no set operations. |
| 763 | It presents a dict-like interface as well with `G.nodes.items()` |
| 764 | iterating over `(node, nodedata)` 2-tuples and `G.nodes[3]['foo']` |
| 765 | providing the value of the `foo` attribute for node `3`. In addition, |
| 766 | a view `G.nodes.data('foo')` provides a dict-like interface to the |
| 767 | `foo` attribute of each node. `G.nodes.data('foo', default=1)` |
| 768 | provides a default for nodes that do not have attribute `foo`. |
| 769 | |
| 770 | Parameters |
| 771 | ---------- |
| 772 | data : string or bool, optional (default=False) |
| 773 | The node attribute returned in 2-tuple (n, ddict[data]). |
| 774 | If True, return entire node attribute dict as (n, ddict). |
| 775 | If False, return just the nodes n. |
| 776 | |
| 777 | default : value, optional (default=None) |
| 778 | Value used for nodes that don't have the requested attribute. |
| 779 | Only relevant if data is not True or False. |
| 780 | |
| 781 | Returns |
| 782 | ------- |
| 783 | NodeView |
| 784 | Allows set-like operations over the nodes as well as node |
| 785 | attribute dict lookup and calling to get a NodeDataView. |
| 786 | A NodeDataView iterates over `(n, data)` and has no set operations. |
| 787 | A NodeView iterates over `n` and includes set operations. |
| 788 | |
| 789 | When called, if data is False, an iterator over nodes. |
| 790 | Otherwise an iterator of 2-tuples (node, attribute value) |
| 791 | where the attribute is specified in `data`. |
| 792 | If data is True then the attribute becomes the |
| 793 | entire data dictionary. |
| 794 | |
| 795 | Notes |
| 796 | ----- |
| 797 | If your node data is not needed, it is simpler and equivalent |
| 798 | to use the expression ``for n in G``, or ``list(G)``. |
| 799 | |
| 800 | Examples |
| 801 | -------- |
| 802 | There are two simple ways of getting a list of all nodes in the graph: |
| 803 | |
| 804 | >>> G = nx.path_graph(3) |
| 805 | >>> list(G.nodes) |
| 806 | [0, 1, 2] |
| 807 | >>> list(G) |
| 808 | [0, 1, 2] |
| 809 | |
| 810 | To get the node data along with the nodes: |
| 811 | |
| 812 | >>> G.add_node(1, time="5pm") |
| 813 | >>> G.nodes[0]["foo"] = "bar" |
| 814 | >>> list(G.nodes(data=True)) |