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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python – 列表 > 原文: [https://howtodoinjava.com/python/python-lists/](https://howtodoinjava.com/python/python-lists/) 在 [Python](https://howtodoinjava.com/python-tutorial/) 中,列表為: * 有序 * 具有索引(索引從 0 開始) * 可變 * 異構的(列表中的項目不必是同一類型) * 寫為方括號之間的逗號分隔值列表 ```py listOfSubjects = ['physics', 'chemistry', "mathematics"] listOfIds = [0, 1, 2, 3, 4] miscList = [0, 'one', 2, 'three'] ``` ## 1.訪問列表項 要訪問列表中的值,請使用切片語法或數組索引形式的方括號來獲取單個項目或項目范圍。 傳遞的索引值可以是正數或負數。 **負索引**值表示從列表末尾開始計數。 `list [m : n]`表示子列表從索引`m`(包括)開始,到索引`n`(不包括)結束。 * 如果未提供`m`,則假定其值為零。 * 如果未提供`n`,則選擇范圍直到列表的最后。 ```py ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print( ids[0] ) # 0 print( ids[1:5] ) # [1, 2, 3, 4] print( ids[ : 3] ) # [0, 1, 2] print( ids[7 : ] ) # [7, 8, 9] print( ids[-8:-5] ) # [2, 3, 4] ``` ## 2.列表 要更改列表中的特定項目,請使用其索引進行引用并分配一個新值。 ```py charList = ["a", "b", "c"] charList [2] = "d" print (charList) # ['a', 'b', 'd'] ``` ## 3.迭代列表 我們可以使用`for`循環遍歷列表項。 ```py charList = ["a", "b", "c"] for x in charList: print(x) # a # b # c ``` ## 4.檢查列表中是否存在項目 使用`'in'`關鍵字確定列表中是否存在指定的項目。 ```py charList = ["a", "b", "c"] if "a" in charList: print("a is present") # a is present if "d" in charList: print("d is present") else: print("d is NOT present") # d is NOT present ``` ## 5.查找列表的長度 使用`len()`函數查找給定列表的長度。 ```py charList = ["a", "b", "c"] x = len (charList) print (x) # 3 ``` ## 6.添加項目 * 要將項目添加到列表的末尾,請使用`append(item)`方法。 * 要在特定索引位置添加項目,請使用`insert(index, item)`方法。 如果`index`大于索引長度,則將項目添加到列表的末尾。 ```py charList = ["a", "b", "c"] charList.append("d") charList.append("e") print (charList) # ['a', 'b', 'c', 'd', 'e'] charList.insert(5, "f") print (charList) # ['a', 'b', 'c', 'd', 'e', 'f'] charList.insert(10, "h") # No error print (charList) # ['a', 'b', 'c', 'd', 'e', 'f', 'h'] ``` ## 7.移除項目 要從列表中刪除一項,請使用以下四種方式之一,即`remove()`,`pop()`,`clear()`或`del`關鍵字。 #### 7.1 `remove()` 它會通過其值刪除指定的項目。 ```py charList = ["a", "b", "c"] charList.remove("c") print (charList) # ['a', 'b'] ``` #### 7.2 `pop()` 它將通過索引刪除指定的項目。 如果未提供`index`,它將從列表中刪除最后一項。 ```py charList = ["a", "b", "c", "d"] charList.pop() # removes 'd' - last item print (charList) # ['a', 'b', 'c'] charList.pop(1) # removes 'b' print (charList) # ['a', 'c'] ``` #### 7.3 `clear()` 它清空列表。 ```py charList = ["a", "b", "c", "d"] charList.clear() print (charList) # [] ``` #### 7.4 `del`關鍵字 它可以用于**通過索引**從列表中刪除某項。 我們也可以使用它來**刪除整個列表**。 ```py charList = ["a", "b", "c", "d"] del charList[0] print (charList) # ['b', 'c', 'd'] del charList print (charList) # NameError: name 'charList' is not defined ``` ## 8.連接兩個列表 我們可以使用`"+"`運算符或`extend()`函數在 Python 中連接兩個給定列表。 ```py charList = ["a", "b", "c"] numList = [1, 2, 3] list1 = charList + numList print (list1) # ['a', 'b', 'c', 1, 2, 3] charList.extend(numList) print (charList) # ['a', 'b', 'c', 1, 2, 3] ``` ## 9\. Python 列表方法 #### 9.1 `append()` 在列表的末尾添加一個元素。 ```py charList = ["a", "b", "c"] charList.append("d") print (charList) # ["a", "b", "c", "d"] ``` #### 9.2 `clear()` 從列表中刪除所有元素。 ```py charList = ["a", "b", "c"] charList.clear() print (charList) # [] ``` #### 9.3 `copy()` 返回列表的副本。 ```py charList = ["a", "b", "c"] newList = charList.copy() print (newList) # ["a", "b", "c"] ``` #### 9.4 `count()` 返回具有指定值的元素數。 ```py charList = ["a", "b", "c"] x = charList.count('a') print (x) # 1 ``` #### 9.5 `extend()` 將列表的元素添加到當前列表的末尾。 ```py charList = ["a", "b", "c"] numList = [1, 2, 3] charList.extend(numList) print (charList) # ['a', 'b', 'c', 1, 2, 3] ``` #### 9.6 `index()` 返回具有指定值的第一個元素的索引。 ```py charList = ["a", "b", "c"] x = charList.index('a') print (x) # 0 ``` #### 9.7 `insert()` 在指定位置添加元素。 ```py charList = ["a", "b", "c"] charList.insert(3, 'd') print (charList) # ['a', 'b', 'c', 'd'] ``` #### 9.8 `pop()` 刪除指定位置或列表末尾的元素。 ```py charList = ["a", "b", "c", "d"] charList.pop() # removes 'd' - last item print (charList) # ['a', 'b', 'c'] charList.pop(1) # removes 'b' print (charList) # ['a', 'c'] ``` #### 9.9 `remove()` 刪除具有指定值的項目。 ```py charList = ["a", "b", "c", "d"] charList.remove('d') print (charList) # ['a', 'b', 'c'] ``` #### 9.10 `reverse()` 反轉列表中項目的順序。 ```py charList = ["a", "b", "c", "d"] charList.reverse() print (charList) # ['d', 'c', 'b', 'a'] ``` #### 9.11 `sort()` 默認情況下,以升序對給定列表進行排序。 ```py charList = ["a", "c", "b", "d"] charList.sort() print (charList) # ["a", "b", "c", "d"] ``` 學習愉快! 閱讀更多: [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>

                              哎呀哎呀视频在线观看