如何删除pythonigraph中的边属性

2024-09-27 07:31:03 发布

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

我想从python-igraph中的图形对象中删除边属性。等价的igraph R函数被巧妙地称为^{}。我在Python中找不到等价的函数/方法。。。有吗?你知道吗

如果没有,还有别的办法吗?(我尝试了一个简单的g.es['edge_attr']= [],但没有成功)。你知道吗

示例代码

g = ig.Graph.Tree(10, 2)        #Generate random graph
g_betweenness = g.betweenness() #Calculate betweenness for graph
g['betweenness'] = betweenness  #Assign betweenness as edge attribute
print(g.attributes())
g['betweenness'] = []           #Attempt to remove betweenness edge attribute (incorrect)
print(g.attributes())

输出

['betweenness']
['betweenness']

所需输出

['betweenness']
[]

Tags: 对象方法函数图形属性esattributeattributes
1条回答
网友
1楼 · 发布于 2024-09-27 07:31:03

您可能无法将其设置为空数组,但您关于直接更改EdgeSequence的直觉已经相当不错了。您只需使用Python的内部del()删除参数,下面我提供了一个最小的示例:

import igraph
# minimal graph
G = igraph.Graph()
G.add_vertices(2)
G.add_edge(0,1)

# add your property
G.es['betweenness'] = [1]

# print edge attribute (note the difference to your example)
print(G.es.attribute_names())
# output: ['weight']

# delete argument
del(G.es['weight'])

# print again
print(G.es.attribute_names())
# output: []

相关问题 更多 >

    热门问题