在列表中查找分钟-python [英] Find min in list - python

查看:58
本文介绍了在列表中查找分钟-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一种Dijkstra作业。

这就是 Vertex 类的外观。

I'm doing a kind of Dijkstra as homework.
This is how the class Vertex looks.

class Vertex:
    def __init__(self, id, name):
        self.id = id
        self.name = name
        self.minDistance = float('inf')
        self.previousVertex = None

在其他课程中,我列出了个未访问的顶点,并且我想找到一个最小距离,因此我可以递归地使用 Vertex 具有 minDistance

In other class I have a list of unvisited vertexes and I want to find a minimum distance, so I can recursively work with the Vertex having that minDistance.

例如未访问= [Vertex1,Vertex2,...]

试图通过做到这一点循环,但是通过迭代并将其保存到变量中无效,因为它仅保存了最后一个值。
如何在列表中找到类属性的最小值?

Tried to do it with for cycle, but by iterating it and saving it to a variable didn't work as it saved only the last value. How can I find a min value of a class atribute in list?

推荐答案

一种更具Python风格的单行代码:

A more Pythonic one-liner:

minDistance = min(otherVertex.distance for otherVertex in unvisited)

这篇关于在列表中查找分钟-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆