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

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

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

        range() 用于浮点数

        时间:2023-11-08

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

              <tbody id='KNy7u'></tbody>
            • <bdo id='KNy7u'></bdo><ul id='KNy7u'></ul>
            • <tfoot id='KNy7u'></tfoot>

                <legend id='KNy7u'><style id='KNy7u'><dir id='KNy7u'><q id='KNy7u'></q></dir></style></legend>

                • 本文介绍了range() 用于浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Is there a range() equivalent for floats in Python?

                  >>> range(0.5,5,1.5)
                  [0, 1, 2, 3, 4]
                  >>> range(0.5,5,0.5)
                  
                  Traceback (most recent call last):
                    File "<pyshell#10>", line 1, in <module>
                      range(0.5,5,0.5)
                  ValueError: range() step argument must not be zero
                  

                  解决方案

                  I don't know a built-in function, but writing one like [this](https://stackoverflow.com/a/477610/623735) shouldn't be too complicated.

                  def frange(x, y, jump):
                    while x < y:
                      yield x
                      x += jump
                  

                  ---

                  As the comments mention, this could produce unpredictable results like:

                  >>> list(frange(0, 100, 0.1))[-1]
                  99.9999999999986
                  

                  To get the expected result, you can use one of the other answers in this question, or as @Tadhg mentioned, you can use decimal.Decimal as the jump argument. Make sure to initialize it with a string rather than a float.

                  >>> import decimal
                  >>> list(frange(0, 100, decimal.Decimal('0.1')))[-1]
                  Decimal('99.9')
                  

                  Or even:

                  import decimal
                  
                  def drange(x, y, jump):
                    while x < y:
                      yield float(x)
                      x += decimal.Decimal(jump)
                  

                  And then:

                  >>> list(drange(0, 100, '0.1'))[-1]
                  99.9
                  

                  [editor's not: if you only use positive jump and integer start and stop (x and y) , this works fine. For a more general solution see here.]

                  这篇关于range() 用于浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么“10000000000000000 在范围内(1000000000000001)"Python 3 这 下一篇:`xrange(2**100)` -&gt;溢出错误:long int 太大而无法转换为 int

                  相关文章

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

                      <small id='8yUxn'></small><noframes id='8yUxn'>

                      <legend id='8yUxn'><style id='8yUxn'><dir id='8yUxn'><q id='8yUxn'></q></dir></style></legend>
                      <tfoot id='8yUxn'></tfoot>