提取节点属性的键和值,并将每个放入单独的字典中,在Python中。

huangapple go评论74阅读模式
英文:

Extracting keys and values of node attributes and placing each in a separate dictionary in Python

问题

这是一个非常简单且可能重复的示例,我无法在我的有限知识中找到答案。首先,我们考虑每个节点的特征,这些特征自然具有值和键。我们已经使用了简单的结构特征,如中心性,其中节点的特征值不同。问题是,根据下面代码的输出,我们想要提取属性的键和值,并将每个键和值分别放入不同的字典中。这意味着键必须在一个字典中,它们的值在另一个字典中。在将它们放入不同的字典之后,我们可以从字典内提取最大值。

在示例代码中,请注意“Continue”命令没有特殊用途,您可以假设我们已经使用了另一个命令,例如“print”。实际上,我们的意思和开始时所说的命令是相同的。

示例的输出如下所示,我们打算在输出上执行开始时所说的操作。

(1, {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793})

你有解决方案吗?

我们猜想我们的输出将类似于以下示例:

{'Number': 1}
{'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793}
英文:

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}

答案1

得分: 1

tpl = (1, {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793})
如果你想要这样的结果:
first_dict = {'number': 1}
second_dict = {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793}
在这种情况下,你已经有了第二个字典。它就是原始元组的第二个元素。
second_dict = tpl[1]
第一个字典就是:
first_dict = {'number': tpl[0]}

英文:

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

huangapple
  • 本文由 发表于 2023年7月3日 04:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定