Any NOUS Any NOUS - 3 months ago 7
Python Question

Alternative to this python code?

I have a line of code from class that I don't understand fully and want some easier alternative to. What this does is , uses weightList, which is a list of edges that's connected to each other, and returns the edgelists with lowest corresponding value from the graph (adjacency matrix). This is for a Prim's Minimum Spanning Tree problem.

edge = sorted(weightList, key=lambda e:graph[e[0]][e[1]])[0];

Answer

Breaking it up a little bit could be enough. How about this?

get_edge_weight = lambda e: graph[e[0]][e[1]]
sorted_weights = sorted(weightList, key=get_edge_weight)
edge = sorted_weights[0]