<bdo id='dYhdX'></bdo><ul id='dYhdX'></ul>

<tfoot id='dYhdX'></tfoot>

<legend id='dYhdX'><style id='dYhdX'><dir id='dYhdX'><q id='dYhdX'></q></dir></style></legend>
  • <i id='dYhdX'><tr id='dYhdX'><dt id='dYhdX'><q id='dYhdX'><span id='dYhdX'><b id='dYhdX'><form id='dYhdX'><ins id='dYhdX'></ins><ul id='dYhdX'></ul><sub id='dYhdX'></sub></form><legend id='dYhdX'></legend><bdo id='dYhdX'><pre id='dYhdX'><center id='dYhdX'></center></pre></bdo></b><th id='dYhdX'></th></span></q></dt></tr></i><div id='dYhdX'><tfoot id='dYhdX'></tfoot><dl id='dYhdX'><fieldset id='dYhdX'></fieldset></dl></div>

      <small id='dYhdX'></small><noframes id='dYhdX'>

        如何在不知道哪个更大的情况下找到两个值之间的差异?

        时间:2023-07-04
        • <bdo id='Tb3Q3'></bdo><ul id='Tb3Q3'></ul>

          <small id='Tb3Q3'></small><noframes id='Tb3Q3'>

            <tfoot id='Tb3Q3'></tfoot>

                <tbody id='Tb3Q3'></tbody>

                <i id='Tb3Q3'><tr id='Tb3Q3'><dt id='Tb3Q3'><q id='Tb3Q3'><span id='Tb3Q3'><b id='Tb3Q3'><form id='Tb3Q3'><ins id='Tb3Q3'></ins><ul id='Tb3Q3'></ul><sub id='Tb3Q3'></sub></form><legend id='Tb3Q3'></legend><bdo id='Tb3Q3'><pre id='Tb3Q3'><center id='Tb3Q3'></center></pre></bdo></b><th id='Tb3Q3'></th></span></q></dt></tr></i><div id='Tb3Q3'><tfoot id='Tb3Q3'></tfoot><dl id='Tb3Q3'><fieldset id='Tb3Q3'></fieldset></dl></div>

              1. <legend id='Tb3Q3'><style id='Tb3Q3'><dir id='Tb3Q3'><q id='Tb3Q3'></q></dir></style></legend>
                  本文介绍了如何在不知道哪个更大的情况下找到两个值之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道 Python 中是否有一个函数可以确定两个有理数之间的距离,但我没有告诉它哪个数字更大.例如

                  I was wondering if there was a function built into Python that can determine the distance between two rational numbers but without me telling it which number is larger. e.g.

                  >>>distance(6,3)
                  3
                  >>>distance(3,6)
                  3
                  

                  显然我可以写一个简单的定义来计算哪个更大,然后做一个简单的减法:

                  Obviously I could write a simple definition to calculate which is larger and then just do a simple subtraction:

                  def distance(x, y):
                      if x >= y:
                          result = x - y
                      else:
                          result = y - x
                      return result
                  

                  但我宁愿不必调用这样的自定义函数.根据我有限的经验,我经常发现 Python 有一个内置函数或模块,可以完全按照您的意愿执行操作,并且比您的代码执行速度更快.希望有人能告诉我有一个内置函数可以做到这一点.

                  but I'd rather not have to call a custom function like this. From my limited experience I've often found Python has a built in function or a module that does exactly what you want and quicker than your code does it. Hopefully someone can tell me there is a built in function that can do this.

                  推荐答案

                  abs(x-y) 将完全满足您的需求:

                  abs(x-y) will do exactly what you're looking for:

                  In [1]: abs(1-2)
                  Out[1]: 1
                  
                  In [2]: abs(2-1)
                  Out[2]: 1
                  

                  这篇关于如何在不知道哪个更大的情况下找到两个值之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:获取Python中整数所需的字节大小 下一篇:如何在 python 脚本中修改系统路径变量?

                  相关文章

                  <small id='KypsW'></small><noframes id='KypsW'>

                  1. <legend id='KypsW'><style id='KypsW'><dir id='KypsW'><q id='KypsW'></q></dir></style></legend>
                      • <bdo id='KypsW'></bdo><ul id='KypsW'></ul>
                      <i id='KypsW'><tr id='KypsW'><dt id='KypsW'><q id='KypsW'><span id='KypsW'><b id='KypsW'><form id='KypsW'><ins id='KypsW'></ins><ul id='KypsW'></ul><sub id='KypsW'></sub></form><legend id='KypsW'></legend><bdo id='KypsW'><pre id='KypsW'><center id='KypsW'></center></pre></bdo></b><th id='KypsW'></th></span></q></dt></tr></i><div id='KypsW'><tfoot id='KypsW'></tfoot><dl id='KypsW'><fieldset id='KypsW'></fieldset></dl></div>

                      <tfoot id='KypsW'></tfoot>