用于动态环形拓扑项目。具体来说,我需要命名每个节点:s1,s2 ... sz,并命名每个主机h1-1,h1-2,... hz-n。所以z是节点的数量,n是连接到每个节点的主机的数量。所以我有一个节点列表,我试图使用该节点作为一个键来获得另一个主机列表,然后我可以将它们放在字典中使用。我该如何实现这一目标?示例图如下:
答案 0 :(得分:0)
我认为你正在寻找类似的东西:
# run with python dynamictopo.py z n
# e.g.: python dynamictopo.py 3 2
import sys
z = int(sys.argv[1]) # number of nodes
n = int(sys.argv[2]) # number of hosts
nodes = []
for i in range(0, z):
nodes.append("s" + str(i + 1))
print(nodes)
dct = {}
for j, node in enumerate(nodes):
hosts = []
for h in range(0, n):
hosts.append("h" + nodes[j][1] + "-" + str(h + 1))
dct[node] = hosts
print(dct)
这将打印['s1','s2','s3']和{'s2':['h2-1','h2-2'],'s3':['h3-1', 'h3-2'],'s1':['h1-1','h1-2']}如果使用3和2作为命令行参数。请注意,dictionaries是无序的。
或者使用它:
# run with python dynamictopo.py z n
# e.g.: python dynamictopo.py 3 2
import sys
z = int(sys.argv[1]) # number of nodes
n = int(sys.argv[2]) # number of hosts
dct = {}
for i in range(z):
hosts = []
for h in range(0, n):
hosts.append("h" + str(i + 1) + "-" + str(h + 1))
dct["s" + str(i + 1)] = hosts
print(dct)