<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python – 元組 > 原文: [https://howtodoinjava.com/python/python-tuples/](https://howtodoinjava.com/python/python-tuples/) 在 [Pyhton](https://howtodoinjava.com/python-tutorial/) 中,**元組**與`list`相似,但它是**不可變的**,并用可選的**圓括號**編寫。 元組是: * 不變的 * 有序 * 異構 * 帶有索引(從零開始) * 帶圓括號(可選,但建議) * 在迭代過程中更快,因為它是不可變的 元組對于創建對象很有用,該對象通常包含相關信息,例如: 員工信息。 換句話說,元組可以讓我們將相關信息“塊”在一起,并將其用作單個事物。 ## 1.創建一個元組 元組中的元素用圓括號括起來,并用逗號分隔。 元組可以包含任意數量的不同類型的項。 ```py Tuple = (item1, item2, item3) ``` ```py tuple1 = () # empty tuple tuple2 = (1, "2", 3.0) tuple3 = 1, "2", 3.0 ``` #### 1.1 一個元素的元組 如果元組僅包含一個元素,則不將其視為元組。 它應該以逗號結尾,以指定解釋器為元組。 ```py tupleWithOneElement = ("hello", ) # Notice trailing comma ``` #### 1.2 嵌套元組 一個包含另一個元組作為元素的元組,稱為嵌套元組。 ```py nestedTuple = ("hello", ("python", "world")) ``` ## 2.訪問元組項 我們可以使用方括號內的索引訪問元組項。 * **正索引**從元組的開始開始計數。 * **負索引**從元組的末尾開始計數。 * 索引的**范圍**將創建帶有指定項目的新元組(稱為切片)。 * 范圍`[m:n]`表示從位置`m`(*包括*)到位置`n`(*排除*)。 * 使用**雙索引**訪問嵌套元組的元素。 ```py Tuple = ("a", "b", "c", "d", "e", "f") print(Tuple[0]) # a print(Tuple[1]) # b print(Tuple[-1]) # f print(Tuple[-2]) # e print(Tuple[0:3]) # ('a', 'b', 'c') print(Tuple[-3:-1]) # ('d', 'e') Tuple = ("a", "b", "c", ("d", "e", "f")) print(Tuple[3]) # ('d', 'e', 'f') print(Tuple[3][0]) # d print(Tuple[3][0:2]) # ('d', 'e') ``` ## 3.遍歷元組 使用`for`循環,以遍歷元組項。 ```py Tuple = ("a", "b", "c") for x in Tuple: print(x) ``` ## 4.檢查項目是否存在于元組中 要檢查元組是否包含給定元素,可以使用`'in'`關鍵字和`'not in'`關鍵字。 ```py Tuple = ("a", "b", "c", "d", "e", "f") if "a" in Tuple: print("Yes, 'a' is present") # Yes, 'a' is present if "p" not in Tuple: print("No, 'p' is not present") # No, 'p' is not present ``` ## 5.對元組進行排序 使用內置的`sorted()`語言方法對元組中的元素進行排序。 ```py Tuple = ("a", "c", "b", "d", "f", "e") sortedTuple = sorted(Tuple) print (sortedTuple) # ("a", "b", "c", "d", "e", "f") ``` ## 6.元組的重復和連接 要重復一個元組的所有元素,請將其乘以所需因子`N`。 ```py Tuple = ("a", "b") repeatedTuple = Tuple * 3 print (repeatedTuple) # ('a', 'b', 'a', 'b', 'a', 'b') ``` 要連接/連接兩個或多個元組,我們可以使用`+`運算符。 ```py Tuple1 = ("a", "b", "c") Tuple2 = ("d", "e", "f") joinedTuple = Tuple1 + Tuple2 print (joinedTuple) # ("a", "b", "c", "d", "e", "f") ``` ## 7.打包和解包元組 **打包**是指我們將一組值分配給變量的操作。 在打包時,元組中的所有項目都分配給一個元組對象。 在下面的示例中,所有三個值都分配給變量`Tuple`。 ```py Tuple = ("a", "b", "c") ``` **解包**稱為將元組變量分配給另一個元組,并將元組中的各個項目分配給各個變量的操作。 在給定的示例中,將元組解包為新的元組,并將值`a, b, c` – 分配給變量`x, y, z`。 ```py Tuple = ("a", "b", "c") # Packing (x, y, z) = Tuple print (x) # a print (y) # b print (z) # c ``` > 在解包期間,分配左側的元組中的元素數必須等于右側的數量。 ```py Tuple = ("a", "b", "c") # Packing (x, y, z) = Tuple # ValueError: too many values to unpack (expected 2) (x, y, z, i) = Tuple # ValueError: not enough values to unpack (expected 4, got 3) ``` ## 8.命名元組 Python 提供了一種稱為`namedtuple()`的**特殊類型的函數**,該函數來自`collection`模塊。 命名元組類似于字典,但是支持從值和鍵訪問,其中字典僅支持按鍵訪問。 ```py import collections Record = collections.namedtuple('Record', ['id', 'name', 'date']) R1 = Record('1', 'My Record', '12/12/2020') #Accessing using index print("Record id is:", R1[0]) # Record id is: 1 # Accessing using key print("Record name is:", R1.name) # Record name is: My Record ``` ## 9\. Python 元組方法 #### 9.1 `any` 如果該元組中至少存在一個元素,則返回`True`;如果該元組為空,則返回`False`。 ```py print( any( () ) ) # Empty tuple - False print( any( (1,) ) ) # One element tuple - True print( any( (1, 2) ) ) # Regular tuple - True ``` #### 9.2 `min()` 返回元組的最小元素(整數)。 ```py Tuple = (4, 1, 2, 6, 9) print( min( Tuple ) ) # 1 ``` #### 9.3 `max()` 返回元組的最大元素(整數)。 ```py Tuple = (4, 1, 2, 6, 9) print( max( Tuple ) ) # 9 ``` #### 9.4 `len()` 返回元組的長度。 ```py Tuple = (4, 1, 2, 6, 9) print( len( Tuple ) ) # 5 ``` #### 9.5 `sum()` 返回元組的所有元素(整數)的總和。 ```py Tuple = (4, 1, 2, 6, 9) print( sum( Tuple ) ) # 22 ``` ## 10.總結 如上所述,元組不可變,有序和索引的異構元素集合。 它寫有或沒有圓括號。 元組對于創建對象類型和實例非常有用。 元組支持類似于[`list`](https://howtodoinjava.com/python/python-lists/)類型的操作,只有我們不能更改元組元素。 學習愉快! 閱讀更多 : [Python – 元組比較](https://howtodoinjava.com/python/compare-tuples/) [Python – 列表與元組](https://howtodoinjava.com/python/lists-vs-tuples/)
                  <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>

                              哎呀哎呀视频在线观看