Add a single node `node_for_adding` and update node attributes. Parameters ---------- node_for_adding : node A node can be any hashable Python object except None. attr : keyword arguments, optional Set or change node attributes using key=value
(self, node_for_adding, **attr)
| 533 | return self.adj[n] |
| 534 | |
| 535 | def add_node(self, node_for_adding, **attr): |
| 536 | """Add a single node `node_for_adding` and update node attributes. |
| 537 | |
| 538 | Parameters |
| 539 | ---------- |
| 540 | node_for_adding : node |
| 541 | A node can be any hashable Python object except None. |
| 542 | attr : keyword arguments, optional |
| 543 | Set or change node attributes using key=value. |
| 544 | |
| 545 | See Also |
| 546 | -------- |
| 547 | add_nodes_from |
| 548 | |
| 549 | Examples |
| 550 | -------- |
| 551 | >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc |
| 552 | >>> G.add_node(1) |
| 553 | >>> G.add_node("Hello") |
| 554 | >>> K3 = nx.Graph([(0, 1), (1, 2), (2, 0)]) |
| 555 | >>> G.add_node(K3) |
| 556 | >>> G.number_of_nodes() |
| 557 | 3 |
| 558 | |
| 559 | Use keywords set/change node attributes: |
| 560 | |
| 561 | >>> G.add_node(1, size=10) |
| 562 | >>> G.add_node(3, weight=0.4, UTM=("13S", 382871, 3972649)) |
| 563 | |
| 564 | Notes |
| 565 | ----- |
| 566 | A hashable object is one that can be used as a key in a Python |
| 567 | dictionary. This includes strings, numbers, tuples of strings |
| 568 | and numbers, etc. |
| 569 | |
| 570 | On many platforms hashable items also include mutables such as |
| 571 | NetworkX Graphs, though one should be careful that the hash |
| 572 | doesn't change on mutables. |
| 573 | """ |
| 574 | if node_for_adding not in self._node: |
| 575 | if node_for_adding is None: |
| 576 | raise ValueError("None cannot be a node") |
| 577 | self._adj[node_for_adding] = self.adjlist_inner_dict_factory() |
| 578 | attr_dict = self._node[node_for_adding] = self.node_attr_dict_factory() |
| 579 | attr_dict.update(attr) |
| 580 | else: # update attr even if node already exists |
| 581 | self._node[node_for_adding].update(attr) |
| 582 | nx._clear_cache(self) |
| 583 | |
| 584 | def add_nodes_from(self, nodes_for_adding, **attr): |
| 585 | """Add multiple nodes. |