python之list,pythonlist

來源:互聯網
上載者:User

python之list,pythonlist

1.python列表

序列是Python中最基本的資料結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推

序列都可以進行的操作包括索引,切片,加,乘,檢查成員。

此外,Python已經內建確定序列的長度以及確定最大和最小的元素的方法。

列表是最常用的Python資料類型,它可以作為一個方括弧內的逗號分隔值出現

列表的資料項目不需要具有相同的類型

>>> l1 = [1,'name','python',88]   #建立列表>>> type(l1)    #查看是否為清單類型<class 'list'>>>> print(l1[2])  #索引列表元素,從0開始,顯示第三個元素也就索引2python>>> print(l1[-1])  #索引最後一個元素88>>> print(l1[1:3])  #元素切片,去序列1到3的值,但不包括3['name', 'python']>>> print(l1[-3:-1]) #取最後三個元素,但不包括最後一個['name', 'python']>>> print(l1[-3:])   #去最後三個元素['name', 'python', 88]>>> print(l1[:])    #去索引值,實際省略了前面的0[1, 'name', 'python', 88]>>> l2 = [l1[2],"very good"]  #索引元素建立新列表>>> print(l2)['python', 'very good']   >>> l2+[1,2,3,4,5]   #連結清單['python', 'very good', 1, 2, 3, 4, 5]列表是可變的,它允許修改元素:>>> l3 = [1,2,3]>>> l3[1]=255    #修改第二個元素為255>>> print(l3)[1, 255, 3]也可以切片賦值清空列表:>>> l4=['a','b','c','d','e']   >>> print(l4)['a', 'b', 'c', 'd', 'e']>>> l4[1:4]=['B','C','D']   #修改索引1-4但不包括4的值>>> print(l4)['a', 'B', 'C', 'D', 'e']>>> l4[1:4]=[]    #清空1-4索引的值,不包括4>>>del l4[1:4]  #也可以使用del刪除列表中的元素>>> print(l4)['a', 'e']>>> l4[0:]=[]    #清空列表,但列表對象還在,只是為空白>>>del l4     #刪除列表,會把整個列表對象刪除>>> print(l4)[]列表允許嵌套列表或元組,字典:>>> l5 = ['a','b','c']>>> l6 = [1,2,3]>>> l7 = [l5,l6]   #使用列表產生嵌套列表>>> l7[['a', 'b', 'c'], [1, 2, 3]]>>> l7[0]     #就是列表了 ['a', 'b', 'c']>>> l7[1][1, 2, 3]>>> l7[0][1]  #嵌套取值,先去列表本身的第1個索引,然後第1個索引元素是列表,可以再索引列表中第2個元素。'b'>>> l7[1][1]2

2、列表基本操作符

len()擷取個數,(+)組合操作,(*)重複操作,(in)判斷操作

>>> l7 =[1,2,3,4,5,6]>>> len(l7)   #列表元素個數6>>> l8 =[1,2,3]+[4,5,6]  #組合列表>>> l8[1, 2, 3, 4, 5, 6]>>> l9=l7*3   #重複列表元素>>> l9[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]>>> 3 in l9    #元素是否在列表中True>>> for i in l7:print(i)  #迭代,迴圈列表元素... 123456

3、python列表函數

cmp,len,max,min,list

#cmp判斷字元在ASCII碼中位置做比較,如a<b返回-1,a>b返回1,a=b返回0In [5]: cmp('a','b')Out[5]: -1In [6]: ord('a')Out[6]: 97In [7]: ord('b')Out[7]: 98In [9]: cmp('b','a')Out[9]: 1In [10]: cmp('a','a')Out[10]: 0#注意:在python3中已經取消cmp函數,使用operator模組代替,在檔案中操作需要匯入模組>>> list1 = [1,2,3,4]>>> len(list1)    #擷取列表元素個數4>>> max(list1)  #擷取列表元素最大值,如果為字串則按ascii碼錶最大值算4>>> min(list1)   #擷取列表元素最小值1>>> tup1 = ('hello','python')>>> list(tup1)    #將元群組轉換為列表['hello', 'python']>>> list2 = list(tup1)>>> type(list2)<class 'list'>>>> list2['hello', 'python']

4、python列表方法

>>> list1[1, 2, 3, 4]>>> list1.append('one')   #在列表最後添加一個對象,可以是字串,列表,元組等>>> print(list1)[1, 2, 3, 4, 'one']>>> list1.append([2,2])>>> print(list1)[1, 2, 3, 4, 'one', [2, 2]]>>> list1.count(2)     #統計某個元素在列表中出現的次數1>>> list1.append(2)>>> list1.count(2)2>>> list1[1, 2, 3, 4, 'one', [2, 2], 2]>>> list1.extend(['two','three','four']) #在列表末尾一次性追加另一個序列中的多個值,可以用列表來擴充原來的列表。>>> list1[1, 2, 3, 4, 'one', [2, 2], 2, 'two', 'three', 'four']>>> list1.index(2)  #在列表中找出某個值第一個匹配項的索引位置1>>> list1.index(2,2,9)  #可以指定索引位置尋找,及從第3個元素開始的第1個2在6的索引位置。6>>> list1.insert(2,('string','tuple','list','doctionary')) #在清單索引2前插入對象元素>>> print(list1)[1, 2, ('string', 'tuple', 'list', 'doctionary'), 3, 4, 'one', [2, 2], 2, 'two', 'three', 'four']>>> list1.pop()  #移除列表中的一個元素,如不指定索引則是移除最後一個,並且返回該元素的值。可以取出該值賦值給新對象'four'>>> list1[1, 2, ('string', 'tuple', 'list', 'doctionary'), 3, 4, 'one', [2, 2], 2, 'two', 'three']>>> list1.pop(2)('string', 'tuple', 'list', 'doctionary')>>> list1[1, 2, 3, 4, 'one', [2, 2], 2, 'two', 'three']>>> list1.remove('three')  #移除列表中某個值的第一匹配項>>> list1[1, 2, 3, 4, 'one', [2, 2], 2, 'two']>>> list1.remove(1)>>> list1[2, 3, 4, 'one', [2, 2], 2, 'two']>>> list1.remove(2)     #只移除第一個匹配值>>> list1[3, 4, 'one', [2, 2], 2, 'two']>>> list1.reverse()    #反向排序列表中的元素>>> list1['two', 2, [2, 2], 'one', 4, 3]>>> l1 = ['a','b','c','a']>>> l1.reverse()>>> l1['a', 'c', 'b', 'a']>>> l1.sort()   #對原列表進行排序>>> l1['a', 'a', 'b', 'c']>>> l2 = [1,3,55,34,22,11,2]>>> l2.sort()>>> l2[1, 2, 3, 11, 22, 34, 55]#注意:在python3中對字元和數子直接排序會報錯,必須指定key排序的關鍵字函數>>> l3 = [1,2,'a','b']>>> l3.sort()Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unorderable types: str() < int()

聯繫我們

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