<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Python 元組 > 原文: [https://www.programiz.com/python-programming/tuple](https://www.programiz.com/python-programming/tuple) #### 在本文中,您將學習有關 Python 元組的所有知識。 更具體地說,什么是元組,如何創建它們,何時使用它們以及您應該熟悉的各種方法。 Python 中的元組類似于[列表](/python-programming/list)。 兩者之間的區別在于,一旦分配了元組,我們就無法更改其元素,而可以更改列表中的元素。 * * * ## 創建一個元組 通過將所有項目(元素)放在圓括號`()`內并用逗號分隔,可以創建一個元組。 括號是可選的,但是,使用它們是一個好習慣。 元組可以具有任意數量的項,并且可以具有不同的類型(整數,浮點數,列表,[字符串](/python-programming/string)等)。 ```py # Different types of tuples # Empty tuple my_tuple = () print(my_tuple) # Tuple having integers my_tuple = (1, 2, 3) print(my_tuple) # tuple with mixed datatypes my_tuple = (1, "Hello", 3.4) print(my_tuple) # nested tuple my_tuple = ("mouse", [8, 4, 6], (1, 2, 3)) print(my_tuple) ``` **輸出** ```py () (1, 2, 3) (1, 'Hello', 3.4) ('mouse', [8, 4, 6], (1, 2, 3)) ``` 也可以在不使用括號的情況下創建元組。 這稱為元組包裝。 ```py my_tuple = 3, 4.6, "dog" print(my_tuple) # tuple unpacking is also possible a, b, c = my_tuple print(a) # 3 print(b) # 4.6 print(c) # dog ``` **輸出**: ```py (3, 4.6, 'dog') 3 4.6 dog ``` 用一個元素創建一個元組有點棘手。 括號內僅包含一個元素是不夠的。 我們將需要一個逗號結尾來表明它實際上是一個元組。 ```py my_tuple = ("hello") print(type(my_tuple)) # <class 'str'> # Creating a tuple having one element my_tuple = ("hello",) print(type(my_tuple)) # <class 'tuple'> # Parentheses is optional my_tuple = "hello", print(type(my_tuple)) # <class 'tuple'> ``` **輸出**: ```py <class 'str'> <class 'tuple'> <class 'tuple'> ``` * * * ## 訪問元組元素 我們可以通過多種方式訪問??元組的元素。 ### 1.索引 我們可以使用索引運算符`[]`訪問元組中的項目,其中索引從 0 開始。 因此,具有 6 個元素的元組將具有從 0 到 5 的索引。嘗試訪問元組索引范圍(在本示例中為 6,7,...)之外的索引將產生`IndexError`。 索引必須是整數,因此我們不能使用`float`或其他類型。 這將導致`TypeError`。 同樣,使用嵌套索引訪問嵌套元組,如下面的示例所示。 ```py # Accessing tuple elements using indexing my_tuple = ('p','e','r','m','i','t') print(my_tuple[0]) # 'p' print(my_tuple[5]) # 't' # IndexError: list index out of range # print(my_tuple[6]) # Index must be an integer # TypeError: list indices must be integers, not float # my_tuple[2.0] # nested tuple n_tuple = ("mouse", [8, 4, 6], (1, 2, 3)) # nested index print(n_tuple[0][3]) # 's' print(n_tuple[1][1]) # 4 ``` **輸出**: ```py p t s 4 ``` * * * ### 2.負索引 Python 允許對其序列進行負索引。 索引 -1 表示最后一項,-2 表示倒數第二項,依此類推。 ```py # Negative indexing for accessing tuple elements my_tuple = ('p', 'e', 'r', 'm', 'i', 't') # Output: 't' print(my_tuple[-1]) # Output: 'p' print(my_tuple[-6]) ``` **輸出**: ```py t p ``` * * * ### 3.切片 我們可以使用切片運算符冒號`:`訪問元組中的一系列項目。 ```py # Accessing tuple elements using slicing my_tuple = ('p','r','o','g','r','a','m','i','z') # elements 2nd to 4th # Output: ('r', 'o', 'g') print(my_tuple[1:4]) # elements beginning to 2nd # Output: ('p', 'r') print(my_tuple[:-7]) # elements 8th to end # Output: ('i', 'z') print(my_tuple[7:]) # elements beginning to end # Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') print(my_tuple[:]) ``` **輸出**: ```py ('r', 'o', 'g') ('p', 'r') ('i', 'z') ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') ``` 通過考慮索引位于元素之間,可以最好地可視化切片,如下所示。 因此,如果要訪問范圍,則需要將元組中的部分切片的索引。 ![Element Slicing in Python](https://img.kancloud.cn/c6/71/c67167494338df35b337599dacf6a127_376x119.png "Element Slicing") Python 中的元素切片 * * * ## 修改元組 與列表不同,元組是不可變的。 這意味著一旦分配了元組的元素就無法更改。 但是,如果元素本身是可變數據類型(如列表),則可以更改其嵌套項目。 我們還可以將元組分配給不同的值(重新分配)。 ```py # Changing tuple values my_tuple = (4, 2, 3, [6, 5]) # TypeError: 'tuple' object does not support item assignment # my_tuple[1] = 9 # However, item of mutable element can be changed my_tuple[3][0] = 9 # Output: (4, 2, 3, [9, 5]) print(my_tuple) # Tuples can be reassigned my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') # Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') print(my_tuple) ``` **輸出**: ```py (4, 2, 3, [9, 5]) ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') ``` 我們可以使用`+`運算符組合兩個元組。 這稱為**連接**。 我們也可以使用`*`運算符將**重復**元組中的元素給定次數。 `+`和`*`操作都產生一個新的元組。 ```py # Concatenation # Output: (1, 2, 3, 4, 5, 6) print((1, 2, 3) + (4, 5, 6)) # Repeat # Output: ('Repeat', 'Repeat', 'Repeat') print(("Repeat",) * 3) ``` **輸出**: ```py (1, 2, 3, 4, 5, 6) ('Repeat', 'Repeat', 'Repeat') ``` * * * ## 刪除元組 如上所述,我們不能更改元組中的元素。 這意味著我們不能刪除或刪除元組中的項目。 但是,可以使用關鍵字[`del`](/python-programming/keyword-list#del)完全刪除一個元組。 ```py # Deleting tuples my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') # can't delete items # TypeError: 'tuple' object doesn't support item deletion # del my_tuple[3] # Can delete an entire tuple del my_tuple # NameError: name 'my_tuple' is not defined print(my_tuple) ``` **輸出**: ```py Traceback (most recent call last): File "<string>", line 12, in <module> NameError: name 'my_tuple' is not defined ``` * * * ## 元組方法 元組不提供添加項目或刪除項目的方法。 僅以下兩種方法可用。 Python 元組方法的一些示例: ```py my_tuple = ('a', 'p', 'p', 'l', 'e',) print(my_tuple.count('p')) # Output: 2 print(my_tuple.index('l')) # Output: 3 ``` **輸出**: ```py 2 3 ``` * * * ## 其他元組操作 ### 1.元組成員資格測試 我們可以使用關鍵字`in`測試項目是否存在于元組中。 ```py # Membership test in tuple my_tuple = ('a', 'p', 'p', 'l', 'e',) # In operation print('a' in my_tuple) print('b' in my_tuple) # Not in operation print('g' not in my_tuple) ``` **輸出**: ```py True False True ``` * * * ### 2.遍歷元組 我們可以使用`for`循環來遍歷元組中的每個項目。 ```py # Using a for loop to iterate through a tuple for name in ('John', 'Kate'): print("Hello", name) ``` **輸出**: ```py Hello John Hello Kate ``` * * * ### 元組優于列表的優勢 由于元組與列表非常相似,因此它們都在類似的情況下使用。 但是,在列表上實現元組具有某些優點。 以下列出了一些主要優點: * 我們通常將元組用于異構(不同)數據類型,將列表用于同類(相似)數據類型。 * 由于元組是不可變的,因此遍歷元組比使用`list`更快。 因此,性能略有提高。 * 包含不可變元素的元組可以用作字典的鍵。 對于列表,這是不可能的。 * 如果您的數據沒有變化,將其實現為元組將保證其保持寫保護。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看