Начиная с графика, которым вы поделились:
know_list = ["a", "b", "c", "d"]
G = nx.Graph()
for i in range(1,5):
G.add_node(i, knowledge = know_list[i-1])
nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')
You could invert the mapping returned by the nx.get_node_attributes
of the knowledge
attribute:
knowledge={j:i for i,j in nx.get_node_attributes(G, 'knowledge').items()}
# {'a': 1, 'b': 2, 'c': 3, 'd': 4}
G.add_node(5, awareness = random.choices(know_list, k=2))
awareness = nx.get_node_attributes(G, 'awareness')
# {5: ['b', 'a']}
Then by iterating over awareness
, you can easily add new edges by looking up in knowledge
the nodes in the inner lists using add_edge
:
for k,v in awareness.items():
for node in v:
G.add_edge(k,knowledge[node])
nx.draw(G, with_labels=True, node_size=500, node_color='lightgreen')
введите описание изображения здесь