0
我已經寫了一個函數,我嘗試調用它一些次,但它返回一個錯誤。Python:使用遞歸時出錯
# create a graph with all active nodes
G = nx.erdos_renyi_graph(100, 0.05)
pos = nx.spring_layout(G)
G.add_nodes_from(G.nodes(), color='blue')
nx.draw_networkx_nodes(G, pos, G.nodes(), node_size=20, node_color='b')
nx.draw_networkx_edges(G, pos, alpha=0.3)
def step(G, x, y):
color = nx.get_node_attributes(G, 'color')
for (key, value) in zip(color, color.values()):
if value == 'blue':
neighbours = G.neighbors(key)
inactive_neighbours = []
for n in neighbours:
if color[n] == 'red':
inactive_neighbours.append(n)
if inactive_neighbours:
chosen_neighbour = random.sample(neighbours, 1)
if np.random.random() < x/(x + y):
activate = []
activate.append(chosen_neighbour)
G.add_nodes_from(activate, color='blue')
if np.random.random() < y/(x + y):
deactivate = []
deactivate.append(key)
G.add_nodes_from(deactivate, color='red')
print(nx.get_node_attributes(G, 'color'))
i = 3
while 2 < i < 100:
step(G, 0.5, 1)
i += 1
step(G, 0.5, 1)
並返回我的錯誤 ValueError: not enough values to unpack (expected 2, got 1)
。 我做錯了什麼? 我如何調用函數來執行它一段時間?
你能給出完整的堆棧跟蹤嗎?無法弄清楚哪一行拋出這個錯誤。 – Afaq
我想看看'zip(color,color.values())'給出了什麼。我懷疑那條線是罪魁禍首。 – karlson
@Afaq它所有代碼,但沒有導入庫 –