python列表簡介

來源:互聯網
上載者:User

標籤:python

是什麼列表:列表讓你能夠在一個地方儲存成組的資訊,其中可以包含幾個元素,也可以包含百萬個元素。列表是新手可直接使用的最輕大的Python功能之一,它融合了眾多重要編程的概念。
列表的定義:列表是由一系列按特定順序排列的元素組成。鑒於列表包含多個元素給列表指定一個表示附屬的名稱(如letters、names)是一個不錯的主意。在Python中用方括弧[ ]來表示列表,並用逗號分割其中的元素。
例如:
[[email protected] ~]# cat bicycles.py bicycles = [‘trek‘,‘cannondale‘,‘redline‘,‘specialized‘]print(bicycles)[[email protected] ~]# python bicycles.py [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]
可以看出上面並不是我們想要看到的,列表列印出來,Python將列印列表的內部顯示,包括方括弧。

1.1.1訪問列表元素
列表是有序集合,因此要訪問列表的任何元素,只需將該元素的位置或索引告訴Python即可。要訪問列表元素,可支出列表的名稱,再指出元素的索引,並將其放在方括弧內。
例如,提取第一個元素[[email protected] ~]# cat bicycles.py bicycles = [‘trek‘,‘cannondale‘,‘redline‘,‘specialized‘]print(bicycles[0])[[email protected] ~]# python bicycles.py trek
1.1.2索引從0而不是從1開始
在Python中,第一個列表元素的索引是0而不是1。在大多數程式設計語言中都是這樣。這與列表操作的底層實現相關。
要訪問列表的任何元素,都可將其位置減1 (n-1)
Python為訪問最後一個列表元素提供了一種特殊文法。通過將索引指定為-1(-2為倒數第二個 -3為第三個,以此類推)
[[email protected] ~]# cat bicycles.py bicycles = [‘trek‘,‘cannondale‘,‘redline‘,‘specialized‘]print(bicycles[-1])[[email protected] ~]# python bicycles.py specialized
這個目的是,不知道列表長度的情況下訪問最後的元素。這種約定也適用於其他負數索引。

1.1.3使用列表中的各個值
從列表中提取一個元素,並用這個值建立一條訊息。
[[email protected] ~]# cat bicycles.py bicycles = [‘trek‘,‘cannondale‘,‘redline‘,‘specialized‘]message = "my first bicycle was a " + bicycles[0].title() + "."print(message)[[email protected] ~]# python bicycles.py my first bicycle was a Trek.
1.2修改、添加和刪除元素
1.2.1修改列表元素
要修改列表元素,可指定列表名和要修改的元素索引,再指定該元素的新值
[[email protected] 列表]# cat modification.pymodification = [‘honda‘,‘yamaha‘,‘suzuki‘]print(modification)modification[0] = ‘xiaoxin‘print(modification)[[email protected] 列表]# python modification.py[‘honda‘, ‘yamaha‘, ‘suzuki‘][‘xiaoxin‘, ‘yamaha‘, ‘suzuki‘]
1.2.2在列表中添加元素
(1)在列表末尾添加元素
方法append()將元素添加到了列表末尾 append:添加的意思
[[email protected] 列表]# cat modification.py modification = [‘honda‘,‘yamaha‘,‘suzuki‘]print(modification)modification[0] = ‘xiaoxin‘print(modification)modification.append(‘shuai‘)print(modification)[[email protected] 列表]# python modification.py[‘honda‘, ‘yamaha‘, ‘suzuki‘][‘xiaoxin‘, ‘yamaha‘, ‘suzuki‘][‘xiaoxin‘, ‘yamaha‘, ‘suzuki‘, ‘shuai‘]
方法append動態建立列表——可以先建立一個空列表,再使用一系列append()語句添加元素。
這種建立方式的好處是,要經常等程式運行後,你才知道使用者要在程式中儲存那些資料,為控制使用者,首先建立一個空列表。用於儲存使用者將要輸入的值。然後將使用者提供的每個區別只顧附加到列表中。
[[email protected] 列表]# cat modification.py message = []message.append(‘shuai‘)message.append(‘xiao‘)message.append(‘xin‘)print(message)[[email protected] 列表]# python modification.py[‘shuai‘, ‘xiao‘, ‘xin‘]
(2)在列表中插入元素
方法insert()在索引0處增加空間,並元素值儲存到這個地方。這種操作將列表中每個元素都右移一個位置
[[email protected] 列表]# cat modification.py message = [‘a‘,‘b‘,‘c‘]message.insert(0,‘cat‘)print(message)[[email protected] 列表]# python modification.py[‘cat‘, ‘a‘, ‘b‘, ‘c‘]
1.2.3從列表中刪除元素
(1)使用del語句刪除元素
如果知道要刪除的元素在列表中的位置,可使用del語句
[[email protected] 列表]# cat modification.py message2 = [‘1‘,‘2‘,‘3‘]print(message2)del message2[2]print(message2)[[email protected] 列表]# python modification.py[‘1‘, ‘2‘, ‘3‘][‘1‘, ‘2‘]
使用del可以刪除任何位置處的列表元素,條件是知道索引。使用del語句從列表中刪除後,你就無法再訪問它了。
(2)pop()刪除元素
有時候你要將元素從列表中刪除,並接著使用它的值用del顯然是不行的
[[email protected] 列表]# cat modification.py message3 = [‘A‘,‘B‘,‘C‘]print(message3)message4 = message3.pop()print(message3)print(message4)[[email protected] 列表]# python modification.py[‘A‘, ‘B‘, ‘C‘][‘A‘, ‘B‘]C
首先定義一個列表並列印,其次將刪除的元素儲存到另一個變數中去,最後列印刪除的元素 也可以根基索引來刪除元素。例如 message4 = message3.pop(2)
1.2.4根據值刪除元素
有時候,你不知道要從列表中刪除的值的所在位置,如果你只知道要刪除的元素的值,可使用remove()
[[email protected] 列表]# python modification.py [‘A‘, ‘B‘, ‘C‘, ‘D‘][‘A‘, ‘C‘, ‘D‘][[email protected] 列表]# cat modification.py message5 = [‘A‘,‘B‘,‘C‘,‘D‘]print(message5)message5.remove(‘B‘)print(message5)

1.3組織列表
1.3.1使用sort()對列表進行永久性排序
在建立列表中,往往需要對元素進行排序。使用sort()對列表進行永久排序。
[[email protected] 列表]# cat cars.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]cars.sort()print(cars)[[email protected] 列表]# python cars.py [‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]
還可以按與字母順序相反的順序排列只需向sort()方法傳遞參數。reverse=True,注意True中的‘T’一定大寫
[[email protected] 列表]# cat cars.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]cars.sort(reverse=True)print(cars)[[email protected] 列表]# python cars.py [‘toyota‘, ‘subaru‘, ‘bmw‘, ‘audi‘]
上面兩種方法對列表的修改順序是永久性的。

1.3.2使用函數sorted()對列表進行臨時排序
[[email protected] 列表]# cat cars1.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]print(sorted(cars))[[email protected] 列表]# python cars1.py [‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]
溫馨提示:
關於順序的修改永久性和臨時性可以這樣理解,當進行排序後永久性生效是下次列印列表時,元素還是按照順序排列,二臨時性二次列印時,順序排列取消。例如:[[email protected] 列表]# cat cars.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]cars.sort()print(cars)print(cars)[[email protected] 列表]# python cars.py [‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘][‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]臨時排序:[[email protected] 列表]# cat cars1.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]print(sorted(cars))print(cars)[[email protected] 列表]# python cars1.py [‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘][‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
1.3.3倒著列印列表
所謂倒著列印,就是反轉列印,不按照字母排序的列印。永久性修改列表元素的排列順序,但可能隨時恢複原來的排列順序,只需要再次調用reverse()即可
[[email protected] 列表]# cat cars.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]cars.reverse()print(cars)[[email protected] 列表]# python cars.py [‘subaru‘, ‘toyota‘, ‘audi‘, ‘bmw‘]
1.3.4確定列表的長度,Python計算資料行表元素時從1開始
[[email protected] 列表]# cat cars.py cars = [‘bmw‘,‘audi‘,‘toyota‘,‘subaru‘]print(len(cars))[[email protected] 列表]# python cars.py 4

python列表簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.