Python 列表(List)

來源:互聯網
上載者:User

標籤:多個   描述   語句   content   strong   sort   括弧   位置   table   

Python 列表(List)

序列是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/pythonlist1 = [‘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]:  physicslist2[1:5]:  [2, 3, 4, 5]
更新列表

你可以對列表的資料項目進行修改或更新,你也可以使用append()方法來添加清單項目,如下所示:

#!/usr/bin/pythonlist = [‘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 :1997New value available at index 2 :2001
刪除列表元素

可以使用 del 語句來刪除列表的的元素,如下執行個體:

#!/usr/bin/pythonlist1 = [‘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 = [‘Google‘, ‘Runoob‘, ‘Taobao‘]>>> L[2]‘Taobao‘>>> L[-2]‘Runoob‘>>> L[1:][‘Runoob‘, ‘Taobao‘]>>> 

描述:

Python 運算式         結果           描述
L[2]            ‘Taobao‘        讀取列表中第三個元素
L[-2]            ‘Runoob‘         讀取列表中倒數第二個元素
L[1:]            [‘Runoob‘, ‘Taobao‘]   從第二個元素開始截取列表

Python列表函數&方法

Python包含以下函數:

序號 函數
1 cmp(list1, list2)
比較兩個列表的元素
2 len(list)
列表元素個數
3 max(list)
返回列表元素最大值
4 min(list)
返回列表元素最小值
5 list(seq)
將元群組轉換為列表

Python包含以下方法:

序號 方法
1 list.append(obj)
在列表末尾添加新的對象
2 list.count(obj)
統計某個元素在列表中出現的次數
3 list.extend(seq)
在列表末尾一次性追加另一個序列中的多個值(用新列表擴充原來的列表)
4 list.index(obj)
從列表中找出某個值第一個匹配項的索引位置
5 list.insert(index, obj)
將對象插入列表
6 list.pop(obj=list[-1])
移除列表中的一個元素(預設最後一個元素),並且返回該元素的值
7 list.remove(obj)
移除列表中某個值的第一個匹配項
8 list.reverse()
反向列表中元素
9 list.sort([func])
對原列表進行排序
 筆記列表

python 建立二維列表,將需要的參數寫入 cols 和 rows 即可

list_2d = [[0 for col in range(cols)] for row in range(rows)]

執行個體:

>>> list_2d = [ [0 for i in range(5)] for i in range(5)]>>> list_2d[0].append(3)>>> list_2d[0].append(5)>>> list_2d[2].append(7)>>> list_2d[[0, 0, 0, 0, 0, 3, 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
#!/usr/bin/python# -*- coding: UTF-8 -*-list01 = [‘runoob‘, 786, 2.23, ‘john‘, 70.2]list02 = [123, ‘john‘]print list01print list02# 列表截取print list01[0]print list01[-1]print list01[0:3]# 列表重複print list01 * 2# 列表組合print list01 + list02# 擷取列表長度print len(list01)# 刪除列表元素del list02[0]print list02# 元素是否存在於列表中print ‘john‘ in list02  # True# 迭代for i in list01:    print i# 比較兩個列表的元素print cmp(list01, list02)# 列表最大/最小值print max([0, 1, 2, 3, 4])print min([0, 1])# 將元群組轉換為列表aTuple = (1,2,3,4)list03 = list(aTuple)print list03# 在列表末尾添加新的元素list03.append(5)print list03# 在列表末尾一次性追加另一個序列中的多個值(用新列表擴充原來的列表)list03.extend(list01)print list03# 統計某個元素在列表中出現的次數print list03.count(1)# 從列表中找出某個值第一個匹配項的索引位置print list03.index(‘john‘)# 將對象插入列表list03.insert(0, ‘hello‘)print list03# 移除列表中的一個元素(預設最後一個元素),並且返回該元素的值print list03.pop(0)print list03# 移除列表中某個值的第一個匹配項list03.remove(1)print list03# 反向列表中元素list03.reverse()print list03# 對原列表進行排序list03.sort()print list03
>>> list4=[123,["das","aaa"],234]>>> list4>>> "aaa" in list4                  #in只能判斷一個層次的元素False>>> "aaa" in list4[1]           #選中列表中的列表進行判斷True>>> list4[1][1]‘aaa‘

 

Python 列表(List)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.