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

        <bdo id='4vSzP'></bdo><ul id='4vSzP'></ul>
      1. <legend id='4vSzP'><style id='4vSzP'><dir id='4vSzP'><q id='4vSzP'></q></dir></style></legend>

        <small id='4vSzP'></small><noframes id='4vSzP'>

      2. 从元组中删除元素时额外的空元素

        extra empty element when removing an element from a tuple(从元组中删除元素时额外的空元素)
          <tfoot id='dvCWM'></tfoot>
          <i id='dvCWM'><tr id='dvCWM'><dt id='dvCWM'><q id='dvCWM'><span id='dvCWM'><b id='dvCWM'><form id='dvCWM'><ins id='dvCWM'></ins><ul id='dvCWM'></ul><sub id='dvCWM'></sub></form><legend id='dvCWM'></legend><bdo id='dvCWM'><pre id='dvCWM'><center id='dvCWM'></center></pre></bdo></b><th id='dvCWM'></th></span></q></dt></tr></i><div id='dvCWM'><tfoot id='dvCWM'></tfoot><dl id='dvCWM'><fieldset id='dvCWM'></fieldset></dl></div>
            <bdo id='dvCWM'></bdo><ul id='dvCWM'></ul>

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

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

                <tbody id='dvCWM'></tbody>

                • 本文介绍了从元组中删除元素时额外的空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对以下 python 结果有疑问.假设我有一个元组:

                  I have a question about the following python outcome. Suppose I have a tuple :

                  a = ( (1,1), (2,2), (3,3) )
                  

                  我想删除 (2,2),我正在使用以下代码:

                  I want to remove (2,2), and I'm doing this with the following code:

                   tuple([x for x in a if x != (2,2)])
                  

                  这很好用,结果是:( (1,1), (3,3) ),正如我所料.

                  This works fine, the result is: ( (1,1), (3,3) ), just as I expect.

                  但假设我从 a = ( (1,1), (2,2) )

                  并使用相同的 tuple() 命令,结果是 ( (1,1), ) 而我希望它是 ((1,1))

                  and use the same tuple() command, the result is ( (1,1), ) while I would expect it to be ((1,1))

                  总之

                  >>> a = ( (1,1), (2,2), (3,3) )
                  >>> tuple([x for x in a if x != (2,2)])
                  ((1, 1), (3, 3))
                  >>> a = ( (1,1), (2,2) )
                  >>> tuple([x for x in a if x != (2,2)])
                  ((1, 1),)
                  

                  为什么在第二种情况下逗号和空元素?我该如何摆脱它?

                  Why the comma and empty element in the second case? And how do I get rid of it?

                  谢谢!

                  推荐答案

                  如果元组只有一个元素,Python 使用尾随逗号:

                  Python uses a trailing comma in case a tuple has only one element:

                  In [21]: type((1,))
                  Out[21]: tuple
                  

                  来自 文档:

                  一个特殊的问题是包含 0 或 1 的元组的构造items:语法有一些额外的怪癖来适应这些.空的元组由一对空括号构成;一个元组一个项目是通过在一个带有逗号的值后面构造的(它不是足以将单个值括在括号中).

                  A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

                  >>> empty = ()
                  >>> singleton = 'hello',    # <-- note trailing comma
                  >>> len(empty)
                  0
                  >>> len(singleton)
                  1
                  >>> singleton
                  ('hello',)
                  

                  这篇关于从元组中删除元素时额外的空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <tbody id='zldqP'></tbody>
                  <legend id='zldqP'><style id='zldqP'><dir id='zldqP'><q id='zldqP'></q></dir></style></legend>
                    <bdo id='zldqP'></bdo><ul id='zldqP'></ul>
                    • <small id='zldqP'></small><noframes id='zldqP'>

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

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