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

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

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

        <legend id='FsK1Y'><style id='FsK1Y'><dir id='FsK1Y'><q id='FsK1Y'></q></dir></style></legend>
        <tfoot id='FsK1Y'></tfoot>
      1. 为什么单个元素元组被解释为python中的那个元素?

        时间:2023-08-31

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

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

                  <tbody id='ZSzzS'></tbody>
                <tfoot id='ZSzzS'></tfoot>
              2. <legend id='ZSzzS'><style id='ZSzzS'><dir id='ZSzzS'><q id='ZSzzS'></q></dir></style></legend>

                  本文介绍了为什么单个元素元组被解释为python中的那个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能解释为什么单元素元组在 Python 中被解释为那个元素?

                  Could anyone explain why single element tuple is interpreted as that element in Python?

                  他们为什么不将元组 (1,) 打印为 (1)?

                  Why don't they just print the tuple (1,) as (1)?

                  请看下面的例子:

                  >>> (1)
                  1
                  >>> ((((1))))
                  1
                  >>> print(1,)
                  1
                  >>> print((1,))
                  (1,)
                  

                  推荐答案

                  单个元素元组永远不会被视为包含的元素.括号主要用于分组,而不是创建元组;逗号就是这样做的.

                  A single element tuple is never treated as the contained element. Parentheses are mostly useful for grouping, not for creating tuples; a comma does that.

                  他们为什么不直接将 (1,) 打印为 (1)?

                  Why don't they just print (1,) as (1)?

                  可能是因为打印内置容器类型提供了一种表示形式,可用于通过 重新创建容器对象,例如 eval:

                  Probably because printing a builtin container type gives a representation that can be used to recreate the container object via , say eval:

                  __repr__的文档a> 对此提供了一些说明:

                  The docs for __repr__ provides some clarity on this:

                  如果可能的话,这应该看起来像一个有效的 Python 表达式可用于重新创建具有相同值的对象

                  If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value

                  回答您的问题,(1) 只是带有分组括号的整数 1.为了通过其表示重新创建单例元组,它必须打印为 (1,) 这是创建元组的有效语法.

                  Answering your question, (1) is just integer 1 with a grouping parenthesis. In order to recreate the singleton tuple via its representation, it has to be printed as (1,) which is the valid syntax for creating the tuple.

                  >>> t = '(1,)'
                  >>> i = '(1)'
                  >>> eval(t)
                  (1,) # tuple
                  >>> eval(i)
                  1    # int
                  

                  这篇关于为什么单个元素元组被解释为python中的那个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将此字符串转换为列表列表? 下一篇:Python:字典列表,如果存在则增加一个字典值,如果不附加一个新字典

                  相关文章

                1. <small id='2qEea'></small><noframes id='2qEea'>

                      <bdo id='2qEea'></bdo><ul id='2qEea'></ul>
                    <legend id='2qEea'><style id='2qEea'><dir id='2qEea'><q id='2qEea'></q></dir></style></legend>

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