Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In networkX python , is it possible to add the same with same ID ? [closed]

What will networkX do if the duplicated nodes (same id , same other attributes )are added? thanks

like image 580
Peter Avatar asked Feb 24 '26 03:02

Peter


1 Answers

If you add a node using a duplicate id with duplicate attributes it will be ignored. However if the attributes are different they will be changed. For example:

>>> import networkx as nx
>>> G = nx.Graph()
>>> G.add_node(1)
>>> G.node[1]
{}
>>> G.add_node(1,name='firstNode')
>>> G.node[1]
{'name': 'firstNode'}
>>> G.add_node(1,name='changed')
>>> G.node[1]
{'name': 'changed'}
>>> G.add_node(1,name='changed',attribute='new')
>>> G.node[1]
{'attribute': 'new', 'name': 'changed'}
>>> G.add_node(1,name='changed',attribute='new')
>>> G.node[1]
{'attribute': 'new', 'name': 'changed'}
>>> G.add_node(1)
>>> G.node[1]
{'attribute': 'new', 'name': 'changed'}
>>> nx.nodes(G)
[1]
like image 128
Joel Green Avatar answered Feb 25 '26 16:02

Joel Green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!