Any NOUS - 1 year ago 64

Python Question

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];`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**