0

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}
Alexander
  • 37
  • 5
  • 1
    Thanks for updating your question with some example data. We can assume that `G.nodes` is some kind of collection / iterable to tuples, where each tuple looks like the one you've provided, yes? I think I don't understand your question - it seems like you just want the maximum `Closeness` and `Betweenness` values, yes? In that case, doesn't the last line of your code give you exactly that? It might help to clarify by providing some more example data, and expected output vs actual output. – Paul M. Jul 02 '23 at 20:31
  • Hello and thank you for your time. You see, dear friend, in fact, our goal is to focus on the characteristics of the nodes and their analysis, and the rest of the issues are secondary issues. In this way, if you pay attention to the obtained output, we want to put the keys, which are the nodes, in one dictionary according to that output, and put their values, which are the characteristic values of the nodes, in another dictionary. – Alexander Jul 02 '23 at 20:36
  • 1
    Could you edit/update your question with an example of what your desired output looks like? – Paul M. Jul 02 '23 at 20:43
  • Suppose you have obtained this output from the code above, but this output does not suit you and you want to convert this output into two dictionaries. How do you convert this output into two dictionaries? – Alexander Jul 02 '23 at 20:48
  • You can imagine that at first they gave you such a code (output code) and they expect you to convert it into two dictionaries, one containing keys and the other containing values. Again, if you do not understand, let me explain. – Alexander Jul 02 '23 at 20:51
  • I'm not sure I understand what you mean by "one containing keys and the other containing values". A single dictionary has keys and values. That's what you store in a dictionary: key-value pairs. So, what keys- and values should the first dictionary have? And what keys- and values should the second dictionary have? If you can share an example of what you want the format to look like, we can share steps to get that desired result. – Paul M. Jul 02 '23 at 20:53
  • In the output above, 1 should be placed in one dictionary and only betweenness centrality and closeness centrality values in another dictionary. – Alexander Jul 02 '23 at 20:54

1 Answers1

1

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]}

Paul M.
  • 10,481
  • 2
  • 9
  • 15