1
在以下圖形實現中,v, w = e
分配的作用是什麼?它如何工作?我認爲我們不允許做這樣的不對稱任務。在Python圖形中添加邊緣(初學者)
class Graph(dict):
def __init__(self, vs=[], es=[]):
"""create a new graph. (vs) is a list of vertices;
(es) is a list of edges."""
for v in vs:
self.add_vertex(v)
for e in es:
self.add_edge(e)
def add_vertex(self, v):
"""add (v) to the graph"""
self[v] = {}
def add_edge(self, e):
"""add (e) to the graph by adding an entry in both directions.
If there is already an edge connecting these Vertices, the
new edge replaces it.
"""
v, w = e
self[v][w] = e
self[w][v] = e
它被稱爲「解包」 - 例如'a,b = [1,2]' – 2013-02-22 21:07:51