NetworkX从特定节点删除属性 [英] NetworkX remove attributes from a specific node

查看:381
本文介绍了NetworkX从特定节点删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中使用networkX库遇到了问题.我建立了一个图 用属性初始化一些节点,边.我还开发了一种将具有特定值的特定属性动态添加到目标节点的方法.例如:

I am having a problem with networkX library in python. I build a graph that initialises some nodes, edges with attributes. I also developed a method that will dynamic add a specific attribute with a specific value to a target node. For example:

 def add_tag(self,G,fnode,attr,value):
    for node in G:
        if node == fnode:
           attrs = {fnode: {attr: value}}
           nx.set_node_attributes(G,attrs)

因此,如果我们打印目标节点的属性,则会对其进行更新

Hence if we print the attributes of the target node will be updated

        print(Graph.node['h1'])

{'color':u'green'}

{'color': u'green'}

        self.add_tag(Graph,'h1','price',40)
        print(Graph.node['h1'])

{'颜色':u'绿色','价格':40}

{'color': u'green', 'price': 40}

我的问题是我该怎么做才能从目标节点中删除现有属性?我找不到删除/删除属性的任何方法.我发现只是.update方法,并没有帮助.

My Question is How can I do the same thing for removing an existing attribute from a target node?? I can't find any method for removing/deleting attributes. I found just .update method and does not helps.

谢谢

推荐答案

属性是python词典,因此您可以使用del删除它们.

The attributes are python dictionaries so you can use del to remove them.

例如

In [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: G.add_node(1,color='red')

In [4]: G.node[1]['shape']='pear'

In [5]: list(G.nodes(data=True))
Out[5]: [(1, {'color': 'red', 'shape': 'pear'})]

In [6]: del G.node[1]['color']

In [7]: list(G.nodes(data=True))
Out[7]: [(1, {'shape': 'pear'})]

这篇关于NetworkX从特定节点删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆