• <legend id='L9C4X'><style id='L9C4X'><dir id='L9C4X'><q id='L9C4X'></q></dir></style></legend>
      <bdo id='L9C4X'></bdo><ul id='L9C4X'></ul>

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

        <i id='L9C4X'><tr id='L9C4X'><dt id='L9C4X'><q id='L9C4X'><span id='L9C4X'><b id='L9C4X'><form id='L9C4X'><ins id='L9C4X'></ins><ul id='L9C4X'></ul><sub id='L9C4X'></sub></form><legend id='L9C4X'></legend><bdo id='L9C4X'><pre id='L9C4X'><center id='L9C4X'></center></pre></bdo></b><th id='L9C4X'></th></span></q></dt></tr></i><div id='L9C4X'><tfoot id='L9C4X'></tfoot><dl id='L9C4X'><fieldset id='L9C4X'></fieldset></dl></div>
        <tfoot id='L9C4X'></tfoot>
      1. Python:从间隔映射到值

        Python: Mapping from intervals to values(Python:从间隔映射到值)
          <bdo id='KqwhO'></bdo><ul id='KqwhO'></ul>
          <i id='KqwhO'><tr id='KqwhO'><dt id='KqwhO'><q id='KqwhO'><span id='KqwhO'><b id='KqwhO'><form id='KqwhO'><ins id='KqwhO'></ins><ul id='KqwhO'></ul><sub id='KqwhO'></sub></form><legend id='KqwhO'></legend><bdo id='KqwhO'><pre id='KqwhO'><center id='KqwhO'></center></pre></bdo></b><th id='KqwhO'></th></span></q></dt></tr></i><div id='KqwhO'><tfoot id='KqwhO'></tfoot><dl id='KqwhO'><fieldset id='KqwhO'></fieldset></dl></div>
            <tbody id='KqwhO'></tbody>
          <legend id='KqwhO'><style id='KqwhO'><dir id='KqwhO'><q id='KqwhO'></q></dir></style></legend>

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

              1. <tfoot id='KqwhO'></tfoot>

                • 本文介绍了Python:从间隔映射到值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在重构一个函数,给定一系列隐式定义区间的端点,检查区间中是否包含一个数字,然后返回一个对应的(不以任何可计算的方式相关).现在处理这项工作的代码是:

                  I'm refactoring a function that, given a series of endpoints that implicitly define intervals, checks if a number is included in the interval, and then return a corresponding (not related in any computable way). The code that is now handling the work is:

                  if p <= 100:
                      return 0
                  elif p > 100 and p <= 300:
                      return 1
                  elif p > 300 and p <= 500:
                      return 2
                  elif p > 500 and p <= 800:
                      return 3
                  elif p > 800 and p <= 1000:
                      return 4
                  elif p > 1000:
                      return 5
                  

                  这在 IMO 中非常可怕,并且缺少间隔和返回值都是硬编码的.当然可以使用任何数据结构.

                  Which is IMO quite horrible, and lacks in that both the intervals and the return values are hardcoded. Any use of any data structure is of course possible.

                  推荐答案

                  import bisect
                  bisect.bisect_left([100,300,500,800,1000], p)
                  

                  这里是文档:bisect

                  这篇关于Python:从间隔映射到值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

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

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

                      <legend id='WugDd'><style id='WugDd'><dir id='WugDd'><q id='WugDd'></q></dir></style></legend>
                    • <tfoot id='WugDd'></tfoot>
                        <tbody id='WugDd'></tbody>
                      • <bdo id='WugDd'></bdo><ul id='WugDd'></ul>