我已经写了一个函数,我试着调用它一些但是它给我一个错误。
# 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)
。
我做错了什么?
我怎样才能调用函数来执行它?