Sets node attributes from a given value or dictionary of values. .. Warning:: The call order of arguments `values` and `name` switched between v1.x & v2.x. Parameters ---------- G : EasyGraph Graph values : scalar value, dict-like What the node attribute should
(G, values, name=None)
| 158 | |
| 159 | |
| 160 | def set_node_attributes(G, values, name=None): |
| 161 | """Sets node attributes from a given value or dictionary of values. |
| 162 | |
| 163 | .. Warning:: The call order of arguments `values` and `name` |
| 164 | switched between v1.x & v2.x. |
| 165 | |
| 166 | Parameters |
| 167 | ---------- |
| 168 | G : EasyGraph Graph |
| 169 | |
| 170 | values : scalar value, dict-like |
| 171 | What the node attribute should be set to. If `values` is |
| 172 | not a dictionary, then it is treated as a single attribute value |
| 173 | that is then applied to every node in `G`. This means that if |
| 174 | you provide a mutable object, like a list, updates to that object |
| 175 | will be reflected in the node attribute for every node. |
| 176 | The attribute name will be `name`. |
| 177 | |
| 178 | If `values` is a dict or a dict of dict, it should be keyed |
| 179 | by node to either an attribute value or a dict of attribute key/value |
| 180 | pairs used to update the node's attributes. |
| 181 | |
| 182 | name : string (optional, default=None) |
| 183 | Name of the node attribute to set if values is a scalar. |
| 184 | |
| 185 | Examples |
| 186 | -------- |
| 187 | After computing some property of the nodes of a graph, you may want |
| 188 | to assign a node attribute to store the value of that property for |
| 189 | each node:: |
| 190 | |
| 191 | >>> G = eg.path_graph(3) |
| 192 | >>> bb = eg.betweenness_centrality(G) |
| 193 | >>> isinstance(bb, dict) |
| 194 | True |
| 195 | >>> eg.set_node_attributes(G, bb, "betweenness") |
| 196 | >>> G.nodes[1]["betweenness"] |
| 197 | 1.0 |
| 198 | |
| 199 | If you provide a list as the second argument, updates to the list |
| 200 | will be reflected in the node attribute for each node:: |
| 201 | |
| 202 | >>> G = eg.path_graph(3) |
| 203 | >>> labels = [] |
| 204 | >>> eg.set_node_attributes(G, labels, "labels") |
| 205 | >>> labels.append("foo") |
| 206 | >>> G.nodes[0]["labels"] |
| 207 | ['foo'] |
| 208 | >>> G.nodes[1]["labels"] |
| 209 | ['foo'] |
| 210 | >>> G.nodes[2]["labels"] |
| 211 | ['foo'] |
| 212 | |
| 213 | If you provide a dictionary of dictionaries as the second argument, |
| 214 | the outer dictionary is assumed to be keyed by node to an inner |
| 215 | dictionary of node attributes for that node:: |
| 216 | |
| 217 | >>> G = eg.path_graph(3) |