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

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

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

        <tfoot id='tjaf8'></tfoot>
      2. Python中方括号和括号括起来的列表有什么区别?

        What#39;s the difference between lists enclosed by square brackets and parentheses in Python?(Python中方括号和括号括起来的列表有什么区别?)
        <tfoot id='CKP2g'></tfoot>
      3. <legend id='CKP2g'><style id='CKP2g'><dir id='CKP2g'><q id='CKP2g'></q></dir></style></legend>
            <tbody id='CKP2g'></tbody>

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

              • <small id='CKP2g'></small><noframes id='CKP2g'>

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

                1. 本文介绍了Python中方括号和括号括起来的列表有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  >>> x=[1,2]
                  >>> x[1]
                  2
                  >>> x=(1,2)
                  >>> x[1]
                  2
                  

                  它们都有效吗?出于某种原因是首选吗?

                  Are they both valid? Is one preferred for some reason?

                  推荐答案

                  方括号是 列表,而括号是元组.

                  Square brackets are lists while parentheses are tuples.

                  列表是可变的,这意味着您可以更改其内容:

                  A list is mutable, meaning you can change its contents:

                  >>> x = [1,2]
                  >>> x.append(3)
                  >>> x
                  [1, 2, 3]
                  

                  虽然元组不是:

                  >>> x = (1,2)
                  >>> x
                  (1, 2)
                  >>> x.append(3)
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  AttributeError: 'tuple' object has no attribute 'append'
                  

                  另一个主要区别是元组是可散列的,这意味着您可以将其用作字典的键等.例如:

                  The other main difference is that a tuple is hashable, meaning that you can use it as a key to a dictionary, among other things. For example:

                  >>> x = (1,2)
                  >>> y = [1,2]
                  >>> z = {}
                  >>> z[x] = 3
                  >>> z
                  {(1, 2): 3}
                  >>> z[y] = 4
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  TypeError: unhashable type: 'list'
                  

                  <小时>

                  请注意,正如许多人所指出的,您可以将元组添加在一起.例如:


                  Note that, as many people have pointed out, you can add tuples together. For example:

                  >>> x = (1,2)
                  >>> x += (3,)
                  >>> x
                  (1, 2, 3)
                  

                  但是,这并不意味着元组是可变的.在上面的例子中,一个 new 元组是通过将两个元组加在一起作为参数来构造的.原始元组未修改.为了证明这一点,请考虑以下几点:

                  However, this does not mean tuples are mutable. In the example above, a new tuple is constructed by adding together the two tuples as arguments. The original tuple is not modified. To demonstrate this, consider the following:

                  >>> x = (1,2)
                  >>> y = x
                  >>> x += (3,)
                  >>> x
                  (1, 2, 3)
                  >>> y
                  (1, 2)
                  

                  然而,如果你用一个列表来构造这个相同的例子,y 也会被更新:

                  Whereas, if you were to construct this same example with a list, y would also be updated:

                  >>> x = [1, 2]
                  >>> y = x
                  >>> x += [3]
                  >>> x
                  [1, 2, 3]
                  >>> y
                  [1, 2, 3]
                  

                  这篇关于Python中方括号和括号括起来的列表有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

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

                        <tbody id='viXFf'></tbody>

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

                      <tfoot id='viXFf'></tfoot>

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