This is a very simple and perhaps repetitive example that I can't find an answer to with my little knowledge. First, we consider features for each node, which naturally have values and keys. We have used simple structural features such as centrality where the feature values are different for nodes. The question is that according to the output of the code below, we want to extract the keys and values of the attributes and put each one in a separate dictionary. This means that the keys must be in one dictionary and their values in another dictionary. After we put them in separate dictionaries, we can extract the maximum value from inside the dictionary.
import networkx as nx
G = nx.read_gml('./networks/karate.gml',label=None)
bb = nx.betweenness_centrality(G)
cc = nx.closeness_centrality(G)
nx.set_node_attributes(G,bb,"Betweenness")
nx.set_node_attributes(G,cc,"Closeness")
for g in G.nodes():
continue
print(max(G.nodes(data=True), key=lambda x: x[1]['Closeness']))
Note that in the sample code, the Continue
command has no special use, and you should assume that we have used another command, for example, print
. In fact, we mean the same commands as we said at the beginning.
The output of the sample is as follows and we are going to do the things we said at the beginning on the output.
(1, {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793})
Do you have a solution?
We guess our output will look something like the following example:
{'Number': 1}
{'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793}
Assuming you have a tuple:
tpl = (1, {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793})
And you want this instead:
first_dict = {'number': 1}
second_dict = {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793}
In this case, you already have your second dictionary. It's just the second element of the original tuple.
second_dict = tpl[1]
The first dict would just be:
first_dict = {'number': tpl[0]}