存储和访问节点属性 python networkx

Storing and Accessing node attributes python networkx(存储和访问节点属性 python networkx)
本文介绍了存储和访问节点属性 python networkx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个使用 python networkx 创建的节点网络.我想在节点中存储信息,以便以后可以根据节点标签(节点的名称)和存储信息的字段(如节点属性)访问信息.存储的信息可以是字符串或数字我希望这样做的方式是,如果 xyz 是一个节点:

然后我想保存两个或三个具有字符串的字段,例如 xyz dob=1185 的出生日期,xyz 的出生地code> pob=usa,以及 xyz dayob=monday 的出生日期.

我知道我可以使用 G.add_node 具有属性字典字段...但我似乎无法访问它的特定字段.如果有其他方法我会很感激的.

然后我想将 xyz 与网络中具有相同信息的其他节点进行比较.即节点 xyz 与节点 abc 的交集基于 bith、出生地点和出生日期

例如,如果节点 xyzabc 有边打印它们各自的 dobs,它们的 pobs和他们的dayobs

解决方案

如你所说,在图中添加节点时添加属性就行了

G.add_node('abc', dob=1185, pob='usa', dayob='monday')

或作为字典

G.add_node('abc', {'dob': 1185, 'pob': 'usa', 'dayob': 'monday'})

要访问属性,只需像使用任何字典一样访问它们

G.node['abc']['dob'] #1185G.node['abc']['pob'] #美国G.node['abc']['dayob'] # 星期一

您说您想查看连接节点的属性.这是一个关于如何实现的小示例.

对于 G.edges_iter() 中的 n1、n2:打印 G.node[n1]['dob'], G.node[n2]['dob']打印 G.node[n1]['pob'], G.node[n2]['pob']# 等等.

从 networkx 2.0 开始,G.edges_iter() 已被 G.edges() 取代.这也适用于节点.我们设置 data=True 来访问属性.现在的代码是:

for n1, n2 in list(G.edges(data=True)):打印 G.node[n1]['dob'], G.node[n2]['dob']打印 G.node[n1]['pob'], G.node[n2]['pob']# 等等.

注意:networkx 2.4 中,G.node[] 已替换为 G.nodes[].

I have a network of nodes created using python networkx. i want to store information in nodes such that i can access the information later based on the node label (the name of the node) and the field that in which the information has been stored (like node attributes). the information stored can be a string or a number I wish to do so in a manner such that if xyz is a node:

then I want to save two or three fields having strings like the date of birth of xyz dob=1185, the place of birth of xyz pob=usa, and the day of birth of xyz dayob=monday.

I know that i can use G.add_node has the attribute dictionary field in it...but I can't seem to access it for a particular field. if there is any other way i would appreciate it.

i then want to compare xyz with other nodes in the networks having the same information in common. i.e. intersection of node xyz with node abc based on date of bith, place of birth and day of birth

e.g for if nodes xyz and abc have an edge print their respective dobs, their pobs and their dayobs

解决方案

As you say, it's just a matter of adding the attributes when adding the nodes to the graph

G.add_node('abc', dob=1185, pob='usa', dayob='monday')

or as a dictionary

G.add_node('abc', {'dob': 1185, 'pob': 'usa', 'dayob': 'monday'})

To access the attributes, just access them as you would with any dictionary

G.node['abc']['dob'] # 1185
G.node['abc']['pob'] # usa
G.node['abc']['dayob'] # monday

You say you want to look at attributes for connected nodes. Here's a small example on how that could be accomplished.

for n1, n2 in G.edges_iter():
    print G.node[n1]['dob'], G.node[n2]['dob']
    print G.node[n1]['pob'], G.node[n2]['pob']
    # Etc.

As of networkx 2.0, G.edges_iter() has been replaced with G.edges(). This also applies to nodes. We set data=True to access attributes. The code is now:

for n1, n2 in list(G.edges(data=True)):
    print G.node[n1]['dob'], G.node[n2]['dob']
    print G.node[n1]['pob'], G.node[n2]['pob']
    # Etc.

NOTE: In networkx 2.4, G.node[] has been replaced with G.nodes[].

这篇关于存储和访问节点属性 python networkx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)