Add a list of edges. Parameters ---------- edges_for_adding : list of 2-element tuple The edges for adding. Each element is a (u, v) tuple, and u, v are two ends of the edge. edges_attr : list of dict, optional The corresponding a
(self, edges_for_adding, edges_attr: List[Dict] = [])
| 860 | self._clear_cache() |
| 861 | |
| 862 | def add_edges(self, edges_for_adding, edges_attr: List[Dict] = []): |
| 863 | """Add a list of edges. |
| 864 | |
| 865 | Parameters |
| 866 | ---------- |
| 867 | edges_for_adding : list of 2-element tuple |
| 868 | The edges for adding. Each element is a (u, v) tuple, and u, v are |
| 869 | two ends of the edge. |
| 870 | |
| 871 | edges_attr : list of dict, optional |
| 872 | The corresponding attributes for each edge in *edges_for_adding*. |
| 873 | |
| 874 | Examples |
| 875 | -------- |
| 876 | Add a list of edges into *G* |
| 877 | |
| 878 | >>> G.add_edges([ |
| 879 | ... (1, 2), |
| 880 | ... (3, 4), |
| 881 | ... ('Jack', 'Tom') |
| 882 | ... ]) |
| 883 | |
| 884 | Add edge with attributes, for example, edge weight, |
| 885 | |
| 886 | >>> G.add_edges([(1,2), (2, 3)], edges_attr=[ |
| 887 | ... { |
| 888 | ... 'weight': 20 |
| 889 | ... }, |
| 890 | ... { |
| 891 | ... 'weight': 15 |
| 892 | ... } |
| 893 | ... ]) |
| 894 | |
| 895 | """ |
| 896 | if edges_attr is None: |
| 897 | edges_attr = [] |
| 898 | if not len(edges_attr) == 0: # Edges attributes included in input |
| 899 | assert len(edges_for_adding) == len( |
| 900 | edges_attr |
| 901 | ), "Edges and Attributes lists must have same length." |
| 902 | else: # Set empty attribute for each edge |
| 903 | edges_attr = [dict() for i in range(len(edges_for_adding))] |
| 904 | |
| 905 | for i in range(len(edges_for_adding)): |
| 906 | try: |
| 907 | edge = edges_for_adding[i] |
| 908 | attr = edges_attr[i] |
| 909 | assert len(edge) == 2, "Edge tuple {} must be 2-tuple.".format(edge) |
| 910 | self._add_one_edge(edge[0], edge[1], attr) |
| 911 | except Exception as err: |
| 912 | print(err) |
| 913 | self._clear_cache() |
| 914 | |
| 915 | def add_edges_from(self, ebunch_to_add, **attr): |
| 916 | """Add all the edges in ebunch_to_add. |