问题描述
在这个问题中,我的数据如下:
number_list = {
'Room 1' : ['Washroom 1','Balcony 1'],'Room 2' : ['gallary 1'],'Room 3' : ['Washroom 2'],'Kitchen' : ['Store 1','Garden 1'],'Garden' : ['Entrance'],'Hall' : ['Store 2']
}
我是从 1 号房间过来的,那里的洗手间 1 和阳台相连。
我想在我想要的地方做一个代码,这些钥匙是:Room1、Room2、Room3、Kitchen、Garden、hall 随机或使用概率相互连接
如何做到这一点?
谢谢
解决方法
如果我正确理解您的问题,以下代码应该可以达到预期目标如果您使用的是 Python 3.6 或更高版本(自 dict
objects preserve order 起)。
import networkx as nx
number_list = {
'Room 1' : ['Washroom 1','Balcony 1'],'Room 2' : ['Gallary 1'],'Room 3' : ['Washroom 2'],'Kitchen' : ['Store 1','Garden 1'],'Garden' : ['Entrance'],'Hall' : ['Store 2']
}
G = nx.Graph(number_list)
# Build sequential pairs of rooms to connect. Python 3.6+ only!
rooms_to_connect = zip(list(number_list.keys())[:-1],list(number_list.keys())[1:])
G.add_edges_from(rooms_to_connect)
nx.draw(G,with_labels=True)