后续:使用NetworkX在Python中添加两个独立图形的边缘连接节点 [英] Follow-up: add edge connecting nodes of two separate graphs in Python using NetworkX

查看:297
本文介绍了后续:使用NetworkX在Python中添加两个独立图形的边缘连接节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当精通C ++,但是我同时学习python,NetworkX和有关pythonic代码的所有知识.阅读各种网站和参考资料后,我的问题与问题.不同之处在于我执行任务所采用的方法.请注意,此问题与添加边有关,而不是绘制 .我有一个图(SAR),其节点也是图(船和漂泊),

I am fairly proficient in C++, but I am learning python, NetworkX, and about pythonic code all at the same time. After reading various websites and the reference my question is the same as this question. The difference is the approach I am taking to implement my task. Please note this question is about adding an edge, not drawing. I have a graph (SAR), whose nodes are also graphs (Boat, and Adrift),

import networkx as nx
import math

# User Inputs
Num_Adrift = 4

# Cluster Definition
Boat = nx.Graph(P_Weight=7000,P_Vol=9*29*60,P_MPG=1,P_Speed=20,P_Crew_Lim=5,P_Asset=1,P_Count=1)
Adrift = nx.Graph(P_Distance=2,P_Time_Lim=30,P_DriftSpeed=1,P_Asset=0,P_Count=Num_Adrift)
SAR = nx.Graph(P_Success = 0)

# SAR Graph
SAR.add_node(Boat)
SAR.add_node(Adrift)

这是在上述问题的注释中引入的方法.我可以使用以下方式将Boat连接到Adrift,

This is the approach introduced in the comments to the above question. I can connect Boat to Adrift using,

SAR.add_edge(Boat,Adrift,weight=1)

假设Boat有一个节点:

Suppose Boat has a node:

Boat.add_node("embark",P_Material=1,C_supply=1)

并且Adrift有节点

and Adrift has the node,

Adrift.add_node("embark",P_lock=1,P_Material=1,C_supply=0)

它们具有相同的名称(目的是在以后自动执行节点连接和计算).

They have the same names (on purpose, to automate node connections and calculations later).

问题1:为什么此语法不起作用,该如何解决?是不是因为它们本身不是SAR内的节点?

Question 1: Why doesn't this syntax work, and how do I fix it? Is it because they are not nodes within SAR per-se?

SAR.add_edge(Boat.node["embark"],Adrift.node["embark"],weight=1)

问题2: NetworkX是否能够区分Boat.node ["embark"]和Adrift.node ["embark"],还是会认为我正在尝试从某个节点获得优势自己?

Question 2: Is NetworkX capable of distinguishing between Boat.node["embark"] and Adrift.node["embark"] or will it think I am trying to make an edge from a node to itself?

我得到的错误是:

---> 76                         SAR.add_edge(Boat.node["embark"],Adrift.node["embark"],weight=1) #<--- this part of code = problem :(

/anaconda3/lib/python3.6/site-packages/networkx/classes/graph.py in add_edge(self, u_of_edge, v_of_edge, **attr)
    873         u, v = u_of_edge, v_of_edge
    874         # add nodes
--> 875         if u not in self._node:
    876             self._adj[u] = self.adjlist_inner_dict_factory()
    877             self._node[u] = {}

TypeError: unhashable type: 'dict'

推荐答案

TypeError:不可散列的类型:'dict'

TypeError: unhashable type: 'dict'

NetworkX使用python字典作为数据结构.因此,当您添加节点时,该节点是dict中给定值的key,例如其边列表.

NetworkX uses python dicts as a data structure. So when you add a node, the node is the key to a given value in the dict, for example its list of edges.

要使对象成为键,它必须是唯一的.要比较键之间的唯一性,对象必须是可哈希的.

For an object to be a key, it must be unique. To compare uniqueness between keys, the object must be hashable.

由于字典不可散列,因此不能将字典用作键,因此不能将字典用作节点.

Since dictionaries are not hashable, you cannot use a dictionary as a key, therefore you cannot use a dictionary as a node.

所能做的就是将整数映射到您的字典,并将其存储在另一个字典中.然后,您可以将整数作为节点,并从另一个字典中获取实际值.

What you can do is to map, say, integers to your dictionaries and store that in another dictionary. Then, you could have the integers as nodes, and get the actual values from the other dict.

这篇关于后续:使用NetworkX在Python中添加两个独立图形的边缘连接节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆