Add nodes with a list of nodes. Parameters ---------- nodes_for_adding : list nodes_attr : list of dict The corresponding attribute for each of *nodes_for_adding*. See Also -------- add_node Examples --------
(self, nodes_for_adding: list, nodes_attr: List[Dict] = [])
| 691 | self._clear_cache() |
| 692 | |
| 693 | def add_nodes(self, nodes_for_adding: list, nodes_attr: List[Dict] = []): |
| 694 | """Add nodes with a list of nodes. |
| 695 | |
| 696 | Parameters |
| 697 | ---------- |
| 698 | nodes_for_adding : list |
| 699 | |
| 700 | nodes_attr : list of dict |
| 701 | The corresponding attribute for each of *nodes_for_adding*. |
| 702 | |
| 703 | See Also |
| 704 | -------- |
| 705 | add_node |
| 706 | |
| 707 | Examples |
| 708 | -------- |
| 709 | Add nodes with a list of nodes. |
| 710 | You can add with node attributes using a list of Python dict type, |
| 711 | each of which is the attribute of each node, respectively. |
| 712 | |
| 713 | >>> G.add_nodes([1, 2, 'a', 'b']) |
| 714 | >>> G.add_nodes(range(1, 200)) |
| 715 | |
| 716 | >>> G.add_nodes(['Jack', 'Tom', 'Lily'], nodes_attr=[ |
| 717 | ... { |
| 718 | ... 'age': 10, |
| 719 | ... 'gender': 'M' |
| 720 | ... }, |
| 721 | ... { |
| 722 | ... 'age': 11, |
| 723 | ... 'gender': 'M' |
| 724 | ... }, |
| 725 | ... { |
| 726 | ... 'age': 10, |
| 727 | ... 'gender': 'F' |
| 728 | ... } |
| 729 | ... ]) |
| 730 | |
| 731 | """ |
| 732 | if not len(nodes_attr) == 0: # Nodes attributes included in input |
| 733 | assert len(nodes_for_adding) == len( |
| 734 | nodes_attr |
| 735 | ), "Nodes and Attributes lists must have same length." |
| 736 | else: # Set empty attribute for each node |
| 737 | nodes_attr = [dict() for i in range(len(nodes_for_adding))] |
| 738 | |
| 739 | for i in range(len(nodes_for_adding)): |
| 740 | try: |
| 741 | self._add_one_node(nodes_for_adding[i], nodes_attr[i]) |
| 742 | except Exception as err: |
| 743 | print(err) |
| 744 | pass |
| 745 | self._clear_cache() |
| 746 | |
| 747 | def add_nodes_from(self, nodes_for_adding, **attr): |
| 748 | """Add multiple nodes. |