Python 串列(list):
1.序列介紹:
序列是Python中最基本的資料結構,序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推,
Python有6個序列的內置型別,但最常見的是串列和元組,
序列都可以進行的操作包括索引,切片,加,乘,檢查成員,
此外,Python已經內置確定序列的長度以及確定最大和最小的元素的方法,
Python:可變序列和不可變序列
?可變序列:串列、集合、字典 可以進行增刪改操作,序列地址頁不會發生變化
?不可變序列:元組、字串 沒有增刪改操作
#可變序列:串列、字典、集合 list_1 = [10,20,30] print(id(list_1)) #list_1的地址為:2927159489792 list_1.append(40) print(id(list_1)) #list_1的地址為:2927159489792 改變了值,串列地址并沒有改變 #不可變序列:字串、元組 str_1 = "HelloWorld" print(id(str_1)) #str_1的地址為:2244899454896 str_1 = str_1+"1" print(id(str_1)) #str_1的地址為:2244900762928 改變了值,字串地址也發生了變化
2.串列的概述:
串列是包含0個或者多個元素的有序序列,屬于序列型別,
特點:
a.串列的長度和內容是可變的(可以自由的對串列中的元素進行,增加、洗掉或者替換)
b.串列沒有長度限制
c.串列中的元素型別可以不同(可以是基本資料型別,也可以是串列、元組、字典、集合以及其他自定義型別的物件)
3.創建一個串列,只要把逗號分隔的不同的資料項使用方括號括起來即可,如下所示:
#創建串列方式一 list_0 = [] #空串列 list_1 = ['elephan','monkey','tiger'] list_2 = [1,2,3] list_3 = [1,2.2,'tiger',True,list_1] #串列可存盤不同的資料型別 #創建串列方式二:使用list()內置函式 list(obj)函式將字串、range()物件、元組等物件轉換為串列, list_4 = list("python") list_5 = list([1,2,3,4,5])
list_6 = list((1,2,3))
list_7 = list(range(1,5,2))
4.串列的索引
#串列的索引 list_1 = [1,2.2,'tiger',True] print(list_1[0]) print(list_1[3]) #print(list_1[4]) 報錯,超出串列的長度
5.串列的分片
#串列的分片 list_1 = [1,2.2,'tiger',True] print(list_1[1:3]) print(list_1[0:3:2]) print(list_1[-1:0:-1]) #反向分片的時候,注意步長要為負數
6.串列的分片賦值
x = [1,2,3,4] x[1] = 1 #替換下角標為1的索引 print(x) x[2:] = 5,6 #從下角標為2的索引開始替換 print(x) x[1:1] = [1,2,3,4,5,6,7] #在變數索引為1的位置插入 “1,2,3,4,5,6,7” print(x) #運行結果 [1, 1, 3, 4] [1, 1, 5, 6] [1, 1, 2, 3, 4, 5, 6, 7, 1, 5, 6]
7.回圈遍歷串列
為了能有效地逐一輸出串列中的資料,可以使用while和for回圈來便利輸出串列,
while回圈遍歷串列:
x = [1,2,3,4,5,6,7,8,9] length = len(x) i = 0 while i<length: print(x[i]) i+=1
for回圈遍歷串列:
x = [1,2,3,4,5,6,7,8,9] for n in x: print(n)
8.查找元素與計數
index(obj)方法:用于回傳指定元素在串列中首次出現的位置,如果該元素不在串列中則拋出例外,
animal = ['elephant','monkey','snake','tiger',(1,2,3)]
print(animal.index((1,2,3)) print(animal.index("snake")) #print(animal.index("喵喵")) #找不到喵喵會拋出例外 x = input("請輸入您要查找的動物") if x in animal: a = animal.index(x) #回傳索引值 print('元素{0}在串列中的索引為:{1}'.format(x,a)) else: print("串列中不存在該元素")
count(obj)方法:統計指定元素在串列中出現的次數
x = [1,2,2,2,3,4,[1,2,3,4],(1,2),(1,2)] a = x.count(2) print(a) b = x.count((1,2)) print(b) #運行結果 3 2
9.串列增加元素:append(obj)方法 extend(seq)方法 insert(index,obj)方法
append(obj):在串列的末尾添加新的元素、物件
add_list = [0,1,2,3,4] add_list.append(5) #在末尾添加一個元素5 print(add_list) add_list.append([6,7]) #在末尾添加一個串列物件[6,7] print(add_list) x = [8,9] add_list.append(x) #在末尾添加一個串列物件x print(add_list) #運行結果 [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, [6, 7]] [0, 1, 2, 3, 4, 5, [6, 7], [8, 9]]
extend(seq):在串列末尾一次性追加另一個序列中的多個值(用新串列拓展原來的串列)
list_1 = [1,2,3] list_2 = [4,5,6] list_1.extend("python") #將序列python與list_1合并 print(list_1) list_1.extend(list_2) #將list_2看作一個序列,將這個序列和list_1合并 print(list_1)
insert(index,obj):可以將指定的物件添加到指定位置
#insert()方法 number = [1,2,4,5] number.insert(2,3) #在索引為2的位置添加元素3 print(number) number.insert(5,[6,7]) #在索引為5的位置添加物件[6,7] print(number) x = [8,9] number.insert(6,x) #在索引為6的位置添加物件x print(number) #運行結果 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, [6, 7]] [1, 2, 3, 4, 5, [6, 7], [8, 9]]
10.串列洗掉元素:del命令 pop()方法 remove()方法
del命令:根據索引洗掉串列中的元素(洗掉一個或者多個)
number = [1,2,3,4,5,6,7,8,9] del number[2] #洗掉索引為2的元素 print(number) del number[1:3] #分片洗掉索引1:3的元素 print(number) del number[-1:0:-2] #分片洗掉索引-1:0:-2的元素 print(number)
pop([obj])方法:用于移除串列中的一個元素(默認最后一個),并且回傳該元素的值 (洗掉一個)
number = [1,2,3,4,5,6,7,8,9] print(number.pop()) #默認洗掉最后一個元素,并回傳該元素的值 print(number) print(number.pop(0) ) #洗掉索引為0的元素 print(number)
remove(obj)方法:移除串列中某個值的第一個匹配項,
#使用remove方法洗掉串列x中的所有'abc' x = ["123",'abc',"ABC",'python','abc'] while 'abc' in x: x.remove('abc') print(x) #運行結果 ['123', 'ABC', 'python']
11.串列排序
reverse()方法:用于將串列中的元素反向存放(修改原串列值),
x = [1,2,3,4] x.reverse() print(x)
#運行結果
[4, 3, 2, 1]
sort([key = None],[reverse = False])方法:用于對原串列進行排序(默認為升序排序),排序后的新串列會覆寫原串列
key:傳遞給key引數的是一個函式,它指定可迭代物件中的每一個元素來按照該函式進行排序
reverse: 表示是否反向排序,默認為False
# 這里先看一個不帶key引數的sort()函式,大家很容易知道結果 li = [[1, 7], [1, 5], [2, 4], [1, 1]] li.sort() print(li) # [[1, 1], [1, 5], [1, 7], [2, 4]] 默認按照0維排序 再按照1維排序 def fun(li): return li[1] # 這時將函式fun傳遞給引數key 得出結果 li.sort(key=fun) print(li) # [[1, 1], [2, 4], [1, 5], [1, 7]]
#是li中每個子元素的第二個數進行排序
#無引數排序 x = [1,2,3,4,5] x.sort() print(x) #指定引數排序,長度,反向 x_1 = ['a','abc','abcd','ab'] x_1.sort(key=len,reverse=True) print(x_1)
#運行結果
[1, 2, 3, 4, 5]
['abcd', 'abc', 'ab', 'a']
sorted(iterable,[key = None],[reverse = False]) 函式:排序方式與sort一樣,但是sorted函式會回傳一個新串列,對原串列不進行修改
iterable:表示可迭代物件,在這里就是串列名,
#無引數排序 x_1 = [1,5,2,3,4] x_2 = sorted(x_1) print(x_1) print(x_2)
#指定引數排序 x_1 = ['a','abc','abcd','ab'] x_2 = sorted(x_1,key=len,reverse=True) print(x_1) print(x_2) #運行結果 [1, 5, 2, 3, 4] [1, 2, 3, 4, 5] ['a', 'abc', 'abcd', 'ab'] ['abcd', 'abc', 'ab', 'a']
Python 元組(tuple):
元組是不可變序列,所以增刪改操作不能使用,
1.為什么要將元組設計成為不可變序列
- 在多任務環境下,同時操作物件不需要加鎖
- 在程式中盡量使用不可變序列
注意:元組中存盤的是物件的參考
- 如果元組中存盤的物件本身為不可變物件,則不能參考其他物件
- 如果元組中存盤的物件本身為可變物件,則可變物件的參考不可改變,但是資料可以
tuple_1 = (10,[20,30],"string") print("1",tuple_1[0],type(tuple_1[0]),id(tuple_1[0])) print("2",tuple_1[1],type(tuple_1[1]),id(tuple_1[1])) print("3",tuple_1[2],type(tuple_1[2]),id(tuple_1[2])) #tuple_1[0] = 1 #tuple_1[1] = 1 這三行代碼報錯,不能修改元組的值 #tuple_1[2] = 1 tuple_1[1].insert(2,40) #使用list的方法修改list的值,但是串列的參考地址并沒有改變 print("4",tuple_1[1],type(tuple_1[1]),id(tuple_1[1])) #運行結果 1 10 <class 'int'> 2698032015952 2 [20, 30] <class 'list'> 2698040063232 3 string <class 'str'> 2698040230832 4 [20, 30, 40] <class 'list'> 2698040063232
2.創建元組
#方式一:直接使用()創建 tuple_1 = (1,2.2,"Python",True) print(tuple_1) #運行結果(1, 2.2, 'Python', True) #()可以省略不寫 tuple_1 = 1,2.22,"Python",True print(tuple_1) #運行結果(1, 2.22, 'Python', True) #方式二:使用內置函式tuple(obj)函式將字串、range()物件、元組等物件轉換為元組 tuple_2 = tuple("python") tuple_3 = tuple([1,2,3,4,5]) tuple_4 = tuple((1,2,3)) tuple_5 = tuple(range(1,5,2)) tuple_6 = tuple({"a":"張三",True:1}) print(tuple_2) #運行結果('p', 'y', 't', 'h', 'o', 'n') print(tuple_3) #運行結果(1, 2, 3, 4, 5) print(tuple_4) #運行結果(1, 2, 3) print(tuple_5) #運行結果(1, 3) print(tuple_6) #運行結果('a', True) #注意:當元組的值,只包含一個元素的時候,一定要加入一個逗號 tuple_7 = (1,) tuple_8 = (1) tuple_9 = ("你",) tuple_10 = ("你") print(tuple_7) #運行結果(1,) print(tuple_8) #運行結果1 print(tuple_9) #運行結果('你',) print(tuple_10) #運行結果你
3.元組的遍歷
tuple_1 = ('python','world',94) #方法一:使用回圈利用索引,獲取全部元組的元素 i = 0 length = len(tuple_1) while i<length: print(tuple_1[i]) i += 1 #方法二 for x in tuple_1: print(x)
4.元組的內置函式
- 序列都會具備的三個函式: len() max() min()
- tuple():功能:以一個序列為引數,并把它轉換為元組,如果引數本身是元組,則原樣回傳該引數(參考上文元組的知識點2,創建元組)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310360.html
標籤:其他
