Python列表List
Python列表 List (可變的):
列表的特點 :
1.其中的元素可以不是同一型別的:
example: list = [1, 2, "Qihe", True]
2.列表中的元素可以替換:
example: list[2] = "sunck"
3.將元組轉換為列表 :
list = list((1,2,3,4,5))
一 . 相關的運算 :
1.列表的組合: list3 = list1 + list2
2.列表的重複: print(list * 3)
3.通過下標訪問列表中的元素: list[2]
4.擷取列表中的某一部分:
list[ : end] list[start : ] lis[start : end]
5.成員關係的判斷: 用成員關係符 in or not in result = 1 in list
6.二維列表: list = [[...], [...] , ...]
訪問: list[1][3]
二 . 相關的函式 :
1.len(list):返回 list 中元素的個數。
2.max(list):返回 list 中最大的元素。
3.min(list):返回 list 中最小的元素。
列表 List自帶函式:
型別一:新增元素
1. append(date):在 List 後面追加元素 date( 注: date 可以使整型,字串, 列表,元組等任意資料型別 ) 。
2.extend(list):將 list 中的元素逐個加入到 List 中去。
3.insert(index, date):在 List 位置 index 處新增元素 date 。
型別二:刪除元素
4.pop( index = -1):在指定位置刪除元素,並且返回元素的值(預設位置在 List 最後)
5.remove(date):移除 List 中的某個元素,從開始到最後匹配到的第一個。
6.clear():清除所有資料。
型別三:查詢元素
7.index(date [, start] [, end]):從列表的指定範圍內,從開頭往後尋找與 date 匹配的值,並且返回第一個匹配值的下標。
8.count(date):返回 date 元素在 List 中出現的次數。
型別四: List的排序
9.reverse(): List倒序。
10.sort(cmp = None, key = None, reverse = False):
cmp:可選引數,指定了引數,會使用該引數的方法進行排序。
key:用來比較的元素,只有一個引數。
reverse:排序規則 True 降序 False 升序
型別五 :其他方法
11.copy():淺拷貝(對應用的拷貝,地址的拷貝):
example:
list1 = [1, 2, 3, 4, 5]
list2 = list1
list2[2] = 4
print(list1)
print(lsit2)
print(id(lsit1) == id(list2))
執行可知:
list1 = [1, 2, 4, 4, 5]
list2 = [1, 2, 4, 4, 5]
True
深拷貝(對記憶體的拷貝):
list1 = [1, 2, 3, 4, 5]
list2 = list1.copy()
list2[2] = 4
print(list1)
print(lsit2)
print(id(lsit1) == id(list2))
執行可知:
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 4, 4, 5]
False