<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?列表(Lists) 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推。 Python有6個序列的內置類型,但最常見的是列表和元組。 序列都可以進行的操作包括索引,切片,加,乘,檢查成員。 此外,Python已經內置確定序列的長度以及確定最大和最小的元素的方法。 列表是最常用的Python數據類型,它可以作為一個方括號內的逗號分隔值出現。 列表的數據項不需要具有相同的類型 創建一個列表,只要把逗號分隔的不同的數據項使用方括號括起來即可。如下所示: ~~~ list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]; ~~~ 與字符串的索引一樣,列表索引從0開始。列表可以進行截取、組合等。 ## 訪問列表中的值 使用下標索引來訪問列表中的值,同樣你也可以使用方括號的形式截取字符,如下所示: ~~~ #!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5] ~~~ 以上實例輸出結果: ~~~ list1[0]: physics list2[1:5]: [2, 3, 4, 5] ~~~ ## 更新列表 你可以對列表的數據項進行修改或更新,你也可以使用append()方法來添加列表項,如下所示: ~~~ #!/usr/bin/python list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : " print list[2]; list[2] = 2001; print "New value available at index 2 : " print list[2]; ~~~ 注意:我們會在接下來的章節討論append()方法的使用 以上實例輸出結果: ~~~ Value available at index 2 : 1997 New value available at index 2 : 2001 ~~~ ## 刪除列表元素 可以使用 del 語句來刪除列表的的元素,如下實例: ~~~ #!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; print list1; del list1[2]; print "After deleting value at index 2 : " print list1; ~~~ 以上實例輸出結果: ~~~ ['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000] ~~~ 注意:我們會在接下來的章節討論remove()方法的使用 ## Python列表腳本操作符 列表對 + 和 * 的操作符與字符串相似。+ 號用于組合列表,* 號用于重復列表。 如下所示: | Python 表達式 | 結果 | 描述 | |--|--|--| | len([1, 2, 3]) | 3 | 長度 | | [1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 組合 | | ['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 重復 | | 3 in [1, 2, 3] | True | 元素是否存在于列表中 | | for x in [1, 2, 3]: print x, | 1 2 3 | 迭代 | ## Python列表截取 Python的列表截取與字符串操作類型,如下所示: ~~~ L = ['spam', 'Spam', 'SPAM!'] ~~~ 操作: | Python 表達式 | 結果 | 描述 | |--|--|--| | L[2] | 'SPAM!' | 讀取列表中第三個元素 | | L[-2] | 'Spam' | 讀取列表中倒數第二個元素 | | L[1:] | ['Spam', 'SPAM!'] | 從第二個元素開始截取列表 | ## Python列表函數&方法 Python包含以下函數: | 序號 | 函數 | |--|--| | 1 | [cmp(list1, list2)](http://www.runoob.com/python/att-list-cmp.html)比較兩個列表的元素 | | 2 | [len(list)](http://www.runoob.com/python/att-list-len.html)列表元素個數 | | 3 | [max(list)](http://www.runoob.com/python/att-list-max.html)返回列表元素最大值 | | 4 | [min(list)](http://www.runoob.com/python/att-list-min.html)返回列表元素最小值 | | 5 | [list(seq)](http://www.runoob.com/python/att-list-list.html)將元組轉換為列表 | Python包含以下方法: | 序號 | 方法 | |--|--| | 1 | [list.append(obj)](http://www.runoob.com/python/att-list-append.html)在列表末尾添加新的對象 | | 2 | [list.count(obj)](http://www.runoob.com/python/att-list-count.html)統計某個元素在列表中出現的次數 | | 3 | [list.extend(seq)](http://www.runoob.com/python/att-list-extend.html)在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表) | | 4 | [list.index(obj)](http://www.runoob.com/python/att-list-index.html)從列表中找出某個值第一個匹配項的索引位置 | | 5 | [list.insert(index, obj)](http://www.runoob.com/python/att-list-insert.html)將對象插入列表 | | 6 | [list.pop(obj=list[-1])](http://www.runoob.com/python/att-list-pop.html)移除列表中的一個元素(默認最后一個元素),并且返回該元素的值 | | 7 | [list.remove(obj)](http://www.runoob.com/python/att-list-remove.html)移除列表中某個值的第一個匹配項 | | 8 | [list.reverse()](http://www.runoob.com/python/att-list-reverse.html)反向列表中元素 | | 9 | [list.sort([func])](http://www.runoob.com/python/att-list-sort.html)對原列表進行排序 |
                  <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>

                              哎呀哎呀视频在线观看