<tfoot id='p7qKf'></tfoot>
    <bdo id='p7qKf'></bdo><ul id='p7qKf'></ul>
    <legend id='p7qKf'><style id='p7qKf'><dir id='p7qKf'><q id='p7qKf'></q></dir></style></legend>

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

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

        如何为新的 python 格式选项设置可变精度

        时间:2023-10-09
      2. <i id='LYfev'><tr id='LYfev'><dt id='LYfev'><q id='LYfev'><span id='LYfev'><b id='LYfev'><form id='LYfev'><ins id='LYfev'></ins><ul id='LYfev'></ul><sub id='LYfev'></sub></form><legend id='LYfev'></legend><bdo id='LYfev'><pre id='LYfev'><center id='LYfev'></center></pre></bdo></b><th id='LYfev'></th></span></q></dt></tr></i><div id='LYfev'><tfoot id='LYfev'></tfoot><dl id='LYfev'><fieldset id='LYfev'></fieldset></dl></div>
        • <legend id='LYfev'><style id='LYfev'><dir id='LYfev'><q id='LYfev'></q></dir></style></legend>
            <tbody id='LYfev'></tbody>

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

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

              • <tfoot id='LYfev'></tfoot>
                  本文介绍了如何为新的 python 格式选项设置可变精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我喜欢包含变量的字符串的新格式选项,但我希望有一个变量来设置整个脚本的精度,但我不知道该怎么做.举个小例子:

                  I am loving the new format option for strings containing variables, but I would like to have a variable that sets the precision through out my script and I am not sure how to do that. Let me give a small example:

                  a = 1.23456789
                  out_str = 'a = {0:.3f}'.format(a)
                  print(out_str)
                  

                  现在这就是我想用伪代码做的事情:

                  Now this is what I would want to do in pseudo code:

                  a = 1.23456789
                  some_precision = 5
                  out_str = 'a = {0:.(some_precision)f}'.format(a)
                  print(out_str)
                  

                  但我不确定,是否可能以及语法是否可能.

                  but I am not sure, if it is possibly and if it is possibly how the syntax would look like.

                  推荐答案

                  您可以嵌套占位符,其中嵌套的占位符可以在格式规范中的任何位置使用:

                  You can nest placeholders, where the nested placeholders can be used anywhere in the format specification:

                  out_str = 'a = {0:.{some_precision}f}'.format(a, some_precision=some_precision)
                  

                  我在那里使用了命名占位符,但您也可以使用编号插槽:

                  I used a named placeholder there, but you could use numbered slots too:

                  out_str = 'a = {0:.{1}f}'.format(a, some_precision)
                  

                  也支持嵌套槽(Python 2.7 及更高版本)的自动编号;编号仍然从左到右进行:

                  Autonumbering for nested slots (Python 2.7 and up) is supported too; numbering still takes place from left to right:

                  out_str = 'a = {0:.{}f}'.format(a, some_precision)
                  

                  嵌套槽先被填充;当前的实现允许您最多嵌套 2 层占位符,因此在占位符中使用占位符是行不通的:

                  Nested slots are filled first; the current implementation allows you to nest placeholders up to 2 levels, so using placeholders in placeholders in placeholders doesn't work:

                  >>> '{:.{}f}'.format(1.234, 2)
                  '1.23'
                  >>> '{:.{:{}}f}'.format(1.234, 2, 'd')
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  ValueError: Max string recursion exceeded
                  

                  您也不能在字段名称中使用占位符(因此不能将值动态分配给插槽).

                  You also can't use placeholders in the field name (so no dynamic allocation of values to slots).

                  这篇关于如何为新的 python 格式选项设置可变精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:加载 svmlight 格式错误 下一篇:格式化多维数组 Python

                  相关文章

                  最新文章

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

                  1. <small id='wrOHQ'></small><noframes id='wrOHQ'>

                      • <bdo id='wrOHQ'></bdo><ul id='wrOHQ'></ul>

                      <tfoot id='wrOHQ'></tfoot>