MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / add_nodes_from

Method add_nodes_from

easygraph/classes/graph.py:747–806  ·  view source on GitHub ↗

Add multiple nodes. Parameters ---------- nodes_for_adding : iterable container A container of nodes (list, dict, set, etc.). OR A container of (node, attribute dict) tuples. Node attributes are updated using the attribute dict

(self, nodes_for_adding, **attr)

Source from the content-addressed store, hash-verified

745 self._clear_cache()
746
747 def add_nodes_from(self, nodes_for_adding, **attr):
748 """Add multiple nodes.
749
750 Parameters
751 ----------
752 nodes_for_adding : iterable container
753 A container of nodes (list, dict, set, etc.).
754 OR
755 A container of (node, attribute dict) tuples.
756 Node attributes are updated using the attribute dict.
757 attr : keyword arguments, optional (default= no attributes)
758 Update attributes for all nodes in nodes.
759 Node attributes specified in nodes as a tuple take
760 precedence over attributes specified via keyword arguments.
761
762 See Also
763 --------
764 add_node
765
766 Examples
767 --------
768 >>> G = eg.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
769 >>> G.add_nodes_from("Hello")
770 >>> K3 = eg.Graph([(0, 1), (1, 2), (2, 0)])
771 >>> G.add_nodes_from(K3)
772 >>> sorted(G.nodes(), key=str)
773 [0, 1, 2, 'H', 'e', 'l', 'o']
774
775 Use keywords to update specific node attributes for every node.
776
777 >>> G.add_nodes_from([1, 2], size=10)
778 >>> G.add_nodes_from([3, 4], weight=0.4)
779
780 Use (node, attrdict) tuples to update attributes for specific nodes.
781
782 >>> G.add_nodes_from([(1, dict(size=11)), (2, {"color": "blue"})])
783 >>> G.nodes[1]["size"]
784 11
785 >>> H = eg.Graph()
786 >>> H.add_nodes_from(G.nodes(data=True))
787 >>> H.nodes[1]["size"]
788 11
789
790 """
791 for n in nodes_for_adding:
792 try:
793 newnode = n not in self._node
794 newdict = attr
795 except TypeError:
796 n, ndict = n
797 newnode = n not in self._node
798 newdict = attr.copy()
799 newdict.update(ndict)
800 if newnode:
801 if n is None:
802 raise ValueError("None cannot be a node")
803 self._adj[n] = self.adjlist_inner_dict_factory()
804 self._node[n] = self.node_attr_dict_factory()

Callers 15

test_isolatesFunction · 0.95
test_number_of_isolatesFunction · 0.95
from_dict_of_listsFunction · 0.45
from_dict_of_dictsFunction · 0.45
_relabel_copyFunction · 0.45
from_numpy_arrayFunction · 0.45
from_scipy_sparse_arrayFunction · 0.45
copyMethod · 0.45
to_directedMethod · 0.45
to_undirectedMethod · 0.45
reverseMethod · 0.45
to_directedMethod · 0.45

Calls 3

_clear_cacheMethod · 0.95
updateMethod · 0.80
copyMethod · 0.45

Tested by 12

test_isolatesFunction · 0.76
test_number_of_isolatesFunction · 0.76
setup_classMethod · 0.36
setup_classMethod · 0.36
test_parse_ucinetMethod · 0.36
test_write_ucinetMethod · 0.36
setup_classMethod · 0.36
setup_classMethod · 0.36