问题描述
我有以下 2 个课程:
class Edge:
a: int
b: int
weight: int
def __init__(self,a: int,b: int,weight: int):
self.a = a
self.b = b
self.weight = weight
和
class Graph:
nodes: Dict[int,Node]
def __init__(self,n):
self.nodes = {i: Node(i) for i in range(n)}
# Returns a list with all edges in the graph
def all_edges(self) -> List[Edge]:
return [Edge(node.id,neighbour,weight) for _,node in self.nodes.items()
for neighbour,weight in node.neighbours.items() if node.id < neighbour]
我想获取所有边的列表,但要根据它们的权重按升序排列。 一种可能的方法是:
# Sort all edges by weight,ascending.
# Note that you Could do this with a loop as well.
edges = sorted(g.all_edges(),key=attrgetter("weight"))
我想知道如何使用循环来做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)