我正在Networkx上构建一个带有评论者及其评论的社交媒体文章数据库的图形。对于每个评论者,我想将名称添加为节点,并将数据库中的某些值添加为属性。但是,在某些情况下,同一评论者名称在同一篇文章中出现两次。然后,Networkx添加此重复的节点,但覆盖原始节点的属性。相反,我想将新属性附加到原始属性上。
请参见下面的代码:
for article in articles_with_comments:
G = nx.Graph()
#start populating graph with nodes and attributes
for j, comment in enumerate(article['comments']):
author = (comment['author'])
causal_relations = (comment['causal_relations'])
G.add_node(author)
if causal_relations is not None:
relations = {author:causal_relations}
nx.set_node_attributes(G, relations,'causal_relations')