python - 找到三个数字的中间

标签 python function python-2.7

我正在寻找替代方法或更简洁的方法来返回三个唯一值的中间值。我现在拥有的是一个函数:

def middle(x, y, z):
    if x > y and x < z:
        return x
    if y > x and y < z:
        return y
    return z

还有更好的吗?

最佳答案

def middle(x, y, z):
    return sorted([x, y, z])[1]

这应该返回中间的数字。但是,如果您实际上是指最大数量

def maximum(x, y, z):
    return max([x, y, z])

编辑:正如 abarnert 在评论部分所建议的,而不是 y>x and x<z使用 x < y < z ,它更具可读性和 pythonic。

关于python - 找到三个数字的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922939/

相关文章:

python - Mac osx Django PIL 错误/usr/bin/cc 重新安装时出现文件错误

python - 想要连接随机均匀生成的值

python - 如何用另一个数组中的值替换包含索引的数组?

python-2.7 - 如何在elastalert中配置Config.yaml?

android - Python 等效于 Java DataInputStream.readUTF()

Python 语言/语法用法

matlab - MATLAB 中隐式函数 f(x,y,z)=0 的轮廓

python - 子类 super 返回 self 应该返回子类

python - 如何不在 Dash 中显示默认的 dcc.graph 模板?

function - 快速匿名功能现在是自动的吗?