python_List,它不是一個簡單的數組


  第一次,學python的時候,我看到列表這個詞語,並不知道這是什么東西。聽名字,感覺很高大上。當學習列表完,原來只不過就是一個數組(數組的升級版)。

  List是python里面最基本的數據結構。序列中每個元素都是從索引(下標)從0開始,依次疊加。

  List操作的方法很多,只能熟悉基本常用的這個方法。

  第二個數據結構是元祖,元組其實跟列表差不多,也是存一組數,只不是它一旦創建,便不能再修改,所以又叫只讀列表。

  元祖的方法只有count和index。

  0.創建

  

1 # list 有兩種創建方式
2 # 第一種
3 list1 = ["1", "2"] 4 # 第一種方式等同於第二鍾方式,其實也是調用第二種方式創建列表
5 # 第二種
6 list(("1", "2")) 7 # 會自動加載構造函數__init__,內部執行for循環,把元祖轉換為列表,相當於創建一個列表

 

 

 

 

  1.切片(獲取多個元素)

 

1 poets = ["libai", "dufu", "luyou", "wangwei", "sushi", "qinguan", "qinshaoyou", "liyu", "yanshu"] 2 poets[1:5]  # 從下標1開始取,至第5個內(不包括5)1 <= x < 5
3 # ['dufu', 'luyou', 'wangwei', 'sushi']
4 poets[-1]  # -1直接獲取最后一個元素
5 # yanshu
6 poets[3:-1]  # 如果想取最后一個元素,不能這樣寫,這樣包含-1在內,應該是這樣寫 poets[3:]
7 # ['wangwei', 'sushi', 'qinguan', 'qinshaoyou', 'liyu']
8 poets[::2]  # 最后一個參數 相當於步長,每隔元素就取一個
9 # ['libai', 'luyou', 'sushi', 'qinshaoyou', 'yanshu']

 

  2.追加(在某尾添加一個元素)

 

1 poets.append("dumu") 2 # ['libai', 'dufu', 'luyou', 'wangwei', 'sushi', 'qinguan', 'qinshaoyou', 'liyu', 'yanshu', 'dumu']
3 # poets = poets.append("dumu") || print(poets.append("dumu")) 該方法沒有返回值,如果這樣操作得到結果為none

 

  3.插入

 

1 sancao = ["caozhi", "coacao", "caopi"] 2 poets.insert(1, sancao) 3 # ['libai', ['caozhi', 'coacao', 'caopi'], 'dufu', 'luyou', 
4 # 'wangwei', 'sushi', 'qinguan', 'qinshaoyou', 'liyu', 'yanshu', 'dumu']
5 # 這個方法需要兩個參數,選擇要插入的位置和要插入的元素 如果只用一個參數,會報錯:insert() takes exactly 2 arguments (1 given)
6 # 第一個參數 只能是數字 不能是字符串 否則會報錯: 'str' object cannot be interpreted as an integer
7 # 第二個參數 可以不填,要填字符串,數字都可以,或者列表等等。。。

 

  4.更新

  

poets[1] = "dufu" # 更新好像沒什么可以說的,就是這樣,選擇對應下標的元素覆蓋,如果括號沒有輸入索引,會報錯語法錯誤 invalid syntax
# poets[] = "ceishi"
# poets['1'] = "ceishi" # 當然索引只能是整形不能是字符串 list indices must be integers or slices, not str

 

  5.移除

 

poets = ["libai", "dufu", "libai", "luyou"] poets.remove("libai") # ['dufu', 'libai', 'luyou'] # 選擇要移除的元素,不是索引,如果有兩個相同元素,只移除第一個匹配項 # 如果不輸入元素,會報錯 x not in list 或者放空 remove() takes exactly one argument (0 given)
poets.pop()  # 默認刪除最后一個元素 # ['dufu', 'libai']
poets = ["libai", "dufu", "luyou", "wangwei", "sushi", "qinguan", "qinshaoyou", "liyu", "yanshu"] poets.pop(4) # ['libai', 'dufu', 'luyou', 'wangwei', 'qinguan', 'qinshaoyou', 'liyu', 'yanshu']
sancao = ["caocao", "caozhi", "caopi"] poets.insert(1, sancao) # ['libai', ['caocao', 'caozhi', 'caopi'], 'dufu', 'luyou', 'wangwei', 'qinguan', 'qinshaoyou', 'liyu', 'yanshu']
poets.pop(1)  # pop可以刪除嵌套中的列表 # ['libai', 'dufu', 'luyou', 'wangwei', 'qinguan', 'qinshaoyou', 'liyu', 'yanshu']
del poets[0:3]  # 刪除列表的元素,此方法可以一次性刪除多個元素,跟切片道理一樣 # ['wangwei', 'qinguan', 'qinshaoyou', 'liyu', 'yanshu']

  

  6.擴展(在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表))

  

1 aList = ["liubei", "guanyu", "zhangfei"] 2 bList = ["zhaoyun", "machao", "huangzhong"] 3 aList.extend(bList) 4 # ['liubei', 'guanyu', 'zhangfei', 'zhaoyun', 'machao', 'huangzhong'] 5 aList.extend("bList") # 如果是字符串,會把字符串分割依次放入列表中 6 # ['liubei', 'guanyu', 'zhangfei', 'b', 'L', 'i', 's', 't'] 7 aList.extend(1) # int' object is not iterable 

  7.統計(統計某個元素在列表中出現的次數)

  

1 List1 = ["a", "v", "b", "a", "a", " ", "",1] 2 List1.count("")  # 統計元素的個數
3 # 3
4 List1.count("")  # 如果不填,應該是統計空格 注意" " 和 ""的區別
5 # 1
6 List1.count(1)  # 也可以統計整形的個數
7 # 1

 

  8.獲取下標

  

1 List1 = ["a", "v", "b", "a", "a", " ", "", 1] 2 List1.index("a")  # 如果有多個相同,至返回第一個元素的下標
3 # 0
4 List1.index("") 5 # 6
6 List1.index("c")  # 如果沒找到,如果沒有找到對象則拋出異常

  

  9.排序&翻轉(對列表的元素進行反向排序)

  

 
         
 1 List3 = ["lin", "huang", "li", 1, 3, 4]  2 # List3.sort() # python 版本問題 3.0版本 不支持 數字和字符串排序 unorderable types: int() < str()
 3 # python 2.0版本支持 數字和字符串排序
 4 List3[3] = '1'
 5 List3[4] = '3'
 6 List3[5] = '4'
 7 List3.sort()  8 # ['1', '3', '4', 'huang', 'li', 'lin']
 9 List3.reverse() 10 List3 11 # ['lin', 'li', 'huang', '4', '3', '1']
 
         

 

 

 

  10.拷貝

  

 1 List1 = ['a', 'b', 'c', 2, 3, 4, [8, 9, 4], 'h']  2 List2 = List1.copy()  # 復制一份出來
 3 List2[0] = 'A'  # copy只能完整把列表第一層數據復制一份
 4 List2[6][0] = 'k'  # 深層里面列表不會完全復制一份,深層列表都指向同一個內存地址。如果深層的數據很大,整個復制不來很占內存
 5 List1  6 List2  7 # ['a', 'b', 'c', 2, 3, 4, ['k', 9, 4], 'h']
 8 # ['A', 'b', 'c', 2, 3, 4, ['k', 9, 4], 'h']
 9 # 如果想實現深層列表完全復制一份,必須調用第三方庫
10 # copy.copy 淺拷貝 只拷貝父對象,不會拷貝對象的內部的子對象。
11 # copy.deepcopy 深拷貝 拷貝對象及其子對象
12 import copy 13 # List3 = List1.deepcopy() # 深copy

 

  11.判斷一個列表是否為空

 

 1 menghuan = ["jianxiage", "xiaoyaosheng", "gujingling", "shentianbing", "longtaizi"]  2 if len(menghuan):  3     print("list have %s element" % len(menghuan))  4 else:  5     print("list is empty")  6 # 由於一個空列表等於false,所以可以簡化len(List)
 7 menghuan = []  8 if menghuan:  9     print("not empty") 10 else: 11     print("empty") 12 
13 
14 list1 = ["a", "b", "c", "d", 2, 3, 4, 2, 4, "ds", 23, 23, 12, "qw", "23"] 15 # 元素是否存在於列表中
16 if 2 in list1: 17     num_of_ele = list1.count(2) 18     position_of_ele = list1.index(2) 19     print(" [%s] is in list1, position[%s]" % (num_of_ele, position_of_ele))

 

 

 

  12.迭代器

  

1 # 遍歷列表同時獲取索引
2 i = 0 3 for element in list1: 4     print(element) 5     i += 1
6 # 既要遍歷索引又要遍歷元素時 這樣寫更簡潔些
7 for i, element in enumerate(list1):  # enumerate是python的內置函數,在字典上是枚舉、列舉的意思
8     print(i) 9     print(element)

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM