TypeError: cannot unpack non-iterable int object

Python报错:TypeError: cannot unpack non-iterable int object

1. Problem description:
When designing the course, it is necessary to dynamically output the dijkstra shortest path search process. During the process, the dynamic change of node and edge colors is needed to show the process of searching for the shortest path. When writing to change the color of one of the sides, the error TypeError: cannot unpack non-iterable int object appeared. A test code is written according to the source code as follows:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.DiGraph()
G.add_edge(1,2)
G.add_edge(4,3)
G.add_edge(1,4)

colors_node=['']*4  #节点颜色
colors_edge=['']*3  #边的颜色

pos=nx.spring_layout(G)

#初始化边的颜色
for i in range(0,3):
    colors_edge[i]='r'


nx.draw(G,pos=pos,with_labels=True,edge_color=colors_edge)
plt.show()
print(G.edges())
a=G.edges()
print(a[0])

Program output: It
Insert picture description here
can be seen that G.edges() is in the format of [()], which is similar to the tuples stored in the list, but the following can not use the index to output the specified value.
After forced type conversion, the index can be output to change the color of the specified edge (according to my idea, if you want to change the color of a specific edge, you need to determine the index of this edge in G, and further determine to change the colors in the The value of which element. I have found a way to directly add color to the edge, but I don’t know why I am reporting an error here, so I thought of this method myself).
The code after the change is as follows:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.DiGraph()
G.add_edge(1,2)
G.add_edge(4,3)
G.add_edge(1,4)

colors_node=['']*4  #节点颜色
colors_edge=['']*3  #边的颜色

pos=nx.spring_layout(G)
#初始化节点和边的颜色
for i in range(0,4):
    colors_node[i]='y'
for i in range(0,3):
    colors_edge[i]='r'


nx.draw(G,pos=pos,with_labels=True,node_color=colors_node,edge_color=colors_edge)
plt.show()
#把G.edge()输出的结果转化为list类型,不然无法实现索引(a[0])
a=list(G.edges())
print(a[0])

The output result is as follows:
Insert picture description here
correct output index value

Guess you like

Origin blog.csdn.net/m0_47470899/article/details/114149092