找出三个数字的中间

编程入门 行业动态 更新时间:2024-10-10 11:30:12
本文介绍了找出三个数字的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找其他或更简洁的方法来返回三个唯一值的中间值.我现在拥有的是一个功能:

I'm looking for alternative or more concise ways to return the middle of three unique values. What I have right now is a function:

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]

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

This should return the middle number. But if you actually meant maximum number

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

编辑:如abarnert在评论部分所建议的那样,请使用 x< x代替 y> x和x< z .&z ,更具可读性和Python风格.

As suggested by abarnert in the comments section, instead of y>x and x<z use x < y < z, which is more readable and pythonic.

更多推荐

找出三个数字的中间

本文发布于:2023-11-29 09:56:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!