Networkx如何在不覆盖属性的情况下为重复节点添加\节点?如何合并属性?

2024-06-06 03:01:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在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')


Tags: innetworkx数据库for属性节点witharticle