Return attribute name Networkx Python

2019-08-02 09:23发布

问题:

With this data:

G.node['node']['a']['initial'] = 100
G.node['node']['a']['final'] = 10

G.node['node']['a']['initial'] = 500
G.node['node']['b']['final'] = 15

I need to do the following calculation:

min(G.node[node]['a']['final'],G.node[node]['b']['final']) / the initial attribute of the node that has the min final value. 
= 10/100

I do not know who to tell python that 10 belongs to the node G.node['node']['a']. Thanks!

回答1:

You can use the key parameter of min function to do this.

min_node = min(['a', 'b'], key=lambda x: G.node['node'][x]['final'])
result = G.node['node'][min_node]['final'] / G.node['node'][min_node]['initial']