列表
1.格式
2.增刪改查
列表下標:
0–n-1
-n-(-1)
#對列表進行切片
#0-(n-1)
#-n-(-1)
list=['dq','python','mm']
print(list[0:2])#[0,2)
print(list[-3:-2])#[-3,-2)
#輸出
#['dq', 'python']
#['dq']
題目:
【1:4:2】:[1,4),步長為2
下標從0開始
3.添加元素
x元組,可迭代對象
#添加元素append
list=['dq','python','mm']
print(list)
list2=['dq','python','mm']
list.append(list2)
print(list)
list.append('2435')
print(list)
#輸出
# ['dq', 'python', 'mm']
# ['dq', 'python', 'mm', ['dq', 'python', 'mm']]
# ['dq', 'python', 'mm', ['dq', 'python', 'mm'], '2435']#添加元素insert
list=['dq','python','mm']
print(list)
list.insert(2,'33')
print(list)
#輸出
#['dq', 'python', 'mm']
#['dq', 'python', '33', 'mm']#添加元素extend
list=['33','jjjk','dq']
print(list)
x=('3243','3454')
list.extend(x)
print(list)
注意:可以直接使用+,連接兩個列表
a=[1,2]
b=[5,6]
c=a+b
print(a,b,c,sep=' ',end='\n')
#[1, 2] [5, 6] [1, 2, 5, 6]
4.刪除元素
#刪除元素pop
list=['dq', 'python', 'mm']
print(list)
list.pop()
print(list)
list.pop(0)
print(list)
#輸出
#['dq', 'python', 'mm']
# ['dq', 'python']
# ['python']#刪除元素remove
list=['dq', 'python', 'mm']
print(list)
list.remove('python')
print(list)
#清空元素clear
list=['dq', 'python', 'mm']
print(list)
list.clear()
print(list)#[]
#清空元素del
list=['dq', 'python', 'mm']
print(list)
del list
print(list)#<class 'list'>
題目:
1)
remove(3):刪除3這個元素
2)
pop(4):刪除下標4對應的元素
從0開始
3)
del刪除下標3到后面全部
下標從0開始
5.修改元素
#修改元素
list=['dq', 'python', 'mm']
list[0]='i love python'
print(list[0])
print(list)
6.查詢元素
(1)index查詢指定數據在列表里面的最小索引,如果沒有,會報錯
(2)count查詢指定數據在列表中出現的次數
(3)in運算符,查詢指定函數是否在列表中,返回布爾數據
#查詢元素
#(1)index查詢指定數據在列表里面的最小索引,如果沒有,會報錯# (2)count查詢指定數據在列表中出現的次數
#
# (3)in運算符,查詢指定函數是否在列表中,返回布爾數據name = ['小陳','小紅','小王','老王','老陳','小劉','小風','小剛','小趙']
print(name.index('小王'))
print(name.count('小王'))
print('趙日天' in name)
#輸出
# 2
# 1
# False
list的地址傳遞
#地址傳遞
num1=[0,0,100]
num2=num1
num1[1]=88
print(num2)
#輸出
#[0, 88, 100]
使用內置函數 id() 可以獲取到一個變量的內存地址
代碼:
#地址傳遞
num1=[0,0,100]
num2=num1
#26983000 26983000
print(id(num1),id(num2),sep=' ')
num1[1]=88
#26983000 26983000
print(id(num1),id(num2),sep=' ')
print(num2)
#輸出
#[0, 88, 100]
list的遍歷
#列表遍歷for
list=['小陳','小紅','小王','老王','老陳','小劉','小風','小剛','小趙']
for i in list:print(i)# 列表遍歷while
i=0
while i<len(list):print(list[i])i+=1
list的排序
使用 list.sort() 會將 list 進行升序排序,返回 NoneType ,影響 list 本身【小-大】
使用 sorted(list) 會將 list 進行升序排序,返回 list ,不影響 list 本身【小-大】
#list排序sort-影響本身
list = ['2341', '962', '67', '34567']
print(list)
list.sort()
print(list)
#list排序sorted-不影響本身
list = ['2341', '962', '67', '34567']
print(list)
print(sorted(list))
print(list)
翻轉:reserve
#list翻轉
list = ['2341', '962', '67', '34567']
print(list)
list.reverse()
print(list)
編程實現冒泡排序算法,實現對[6, 5, 3, 1, 8, 7, 2, 4]的排序。
思想:讓一個數字和它相鄰的下一個數字進行比較運算,如果前一個數字大于后一個數字,則交換兩個數據的位置。
#列表冒泡排序
#按第一個字符ASCII碼排序
list=['2341','962','67','34567']print(list)
for i in range(len(list)-1):for j in range(len(list)-1-i):if list[j]>list[j+1]:temp=list[j]list[j]=list[j+1]list[j+1]=tempprint(list)
去除列表重復元素
1、使用set函數
set是定義集合的,無序,非重復
numList = [1,1,2,3,4,5,4]
print(list(set(numList)))
#[1, 2, 3, 4, 5]
2、先把list重新排序,然后從list的最后開始掃描
a = [1, 2, 4, 2, 4, 5,]
a.sort()
last = a[-1]
for i in range(len(a) - 2, -1, -1):if last == a[i]:del a[i]else:last = a[i]
print(a) #[1, 2, 4, 5]
3、使用字典函數
a=[1,2,4,2,4,]b={}b=b.fromkeys(a)c=list(b.keys())print(c) #[1, 2, 4]
4、append方式
def delList(L):L1 = []for i in L:if i not in L1:L1.append(i)return L1
print(delList([1, 2, 2, 3, 3, 4, 5])) #[1, 2, 3, 4, 5]
5、count + remove方式
def delList(L):for i in L:if L.count(i) != 1:for x in range((L.count(i) - 1)):L.remove(i)return L
print(delList([1, 2, 2, 3, 3, 4]))#[1, 2, 3, 4]
列表的嵌套
random.randint(a,b):[a,b]
import random
#列表的嵌套
teacher = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
room = [[], [], []]
for i in teacher:room[random.randint(0, 2)].append(i)
print(room)
#輸出:
#[[2, 10], [6], [1, 3, 4, 5, 7, 8, 9]]
列表推導式
range(n):[0,n-1]
#列表推導式
list=[i for i in range(11)]
print(list)
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
列表的復制
淺拷貝是在另一塊地址中創建一個新的變量或容器,但是容器內的元素的地址均是源對象的元素的地址的拷貝。也就是說新的容器中指向了舊的元素( 新瓶裝舊酒 )。
深拷貝,完全拷貝了一個副本,容器內部元素地址都不一樣
淺拷貝:a=b
copy(list)
copy.copy(list)
深拷貝:
copy.deepcopy(list)
#列表的復制b=a,淺拷貝
a=[1,2,'P']
b=a
b[0]='dq'
print(a,id(a),sep=' ',end='\n')
print(b,id(b),sep=' ',end='\n')
#地址空間指向一樣
#[1, 2, 'P'] 29181352
#[1, 2, 'P'] 29181352#列表的復制copy1,淺拷貝,指拷貝了一層
a=[1,2,'P']
b=a.copy()
b[0]='dq'
print(a,id(a),sep=' ',end='\n')
print(b,id(b),sep=' ',end='\n')
#地址空間指向不同
# [1, 2, 3] 20006312
# [1, 2, 3] 20007472#列表的復制copy2,淺拷貝,指拷貝了一層
a=[1,2,3,[4,'V','B']]
b=a.copy()
b[0]='dq'
print(a,id(a),sep=' ',end='\n')
print(b,id(b),sep=' ',end='\n')
#地址空間指向不同
# [1, 2, 3] 20006312
# [1, 2, 3] 20007472#列表的復制copy.copy1,淺拷貝,指拷貝了一層
import copy
a=[1,2,'A']
b=copy.copy(a)
b[0]='dq'
print(a,id(a),sep=' ',end='\n')
print(b,id(b),sep=' ',end='\n')
#地址空間指向不同
# [1, 2, 3] 23106584
# [1, 2, 3] 19097440#列表的復制copy.copy2,深拷貝
import copy
a=[1,2,3,['U','X',7]]
b=copy.copy(a)
b[0]='dq'
print(a,id(a),sep=' ',end='\n')
print(b,id(b),sep=' ',end='\n')
#地址空間指向不同
# [1, 2, 3] 23106584
# [1, 2, 3] 19097440#列表的復制copy.deepcopy,深拷貝
import copy
a=[1,2,3,['U','O',7]]
b=copy.deepcopy(a)
b[0]='dq'
print(a,id(a),sep=' ',end='\n')
print(b,id(b),sep=' ',end='\n')
題目:
淺拷貝,地址空間指向一樣
列表和字符串的互相轉換
同時輸出列表和字符串
1.字符串>列表:split()a = 'my first python'b = a.split(" ")print(b)
輸出:2.列表>字符串:join()a = ['my', 'first', 'python']b = ' '.join(a)print(b)
輸出:
示例:
def __str__(self):#print('style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+str(self.furnituresList) + '\n')#return 'style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+str(self.furnituresList) + '\n'return 'style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+' '.join(self.furnituresList) + '\n'
練習1-求列表中最大值及下標
#求列表中最大值及下標-sorted
list=[1,345,32435,78,324,7685]
list2=sorted(list)
print(('最大值=%d 下標=%d')%(list2[len(list2)-1],list.index(list2[len(list2)-1])))
#求列表中最大值及下標-for
list = [1, 345, 32435, 78, 324, 7685]
max = index = 0
for i in list:max=max if max >i else iindex=list.index(max)
print(('最大值=%d 下標=%d')%(max,index))
練習2-移除列表空字符串
#移除列表空字符串remove()-每次只移除一個
list = ['小陳', '小紅', '', '小王', "", '老王', '老陳', "", '小劉', '小風', '小剛', '小趙']
print(list)
for i in list:if i=='':list.remove('')
print(list)
練習3-計算 chars 列表中每個字符出現的次數
#1
#計算 chars 列表中每個字符出現的次數。chars = ['a', 'c', 'x', 'd', 'p', 'a', 'm', 'q', 's', 't', 'a', 'c']
chars = ['a', 'c', 'x', 'd', 'p', 'a', 'm', 'q', 's', 't', 'a', 'c']
list = []
for i in chars:check = i + "=" + str(chars.count(i))if (check in list) == False:list.append(check)
print(list)