<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() 用于浮点数

        range() for floats(range() 用于浮点数)

            <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() 用于浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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中的另一个文件中查找一个文本文件的条目)
                    <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>
                            <tbody id='8yUxn'></tbody>
                            <tfoot id='8yUxn'></tfoot>