1.列表 list[ ]
列表是python中的基礎數據類型之一,其他語言中也有類似於列表的數據類型,比如js中叫數組,他是以[ ] 括起來,每個元素以逗號隔開,而且他里面可以存放各種數據類型比如:li = [‘span’,123,True,(1,2,3,’python’),[1,2,3,’小明’,],{‘name’:’span’}]
列表相比於字符串,不僅可以儲存不同的數據類型,而且可以儲存大量數據,32位python的限制是 536870912 個元素,64位python的限制是 1152921504606846975 個元素。而且列表是有序的,有索引值,可切片,方便取值。
1.1 列表有序,意味着可以用索引、切片取數據,也可以跳着切片。
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] l1 = li[2] l2 = li[1] print(l1) print(l2)
l3 = li[0:3]
print(l3)
>>>python
>>>[18, 188, 1888]
>>>['span', [18, 188, 1888], 'python']
1.2 #增加
list.append() 增加到最后
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li.append("Math") print(li) >>>['span', [18, 188, 1888], 'python', '金融', 'FX-EXchange', 'Math']
拓展:持續向列表里添加元素,按“Q”退出
while 1: username = input(">>>:") if username.strip().upper() == "Q": break else: li.append(username) print(li)
# 插入 list.insert(索引,插入的值)
#添加 list.extend("iterable") 迭代的添加,將添加的值分成可迭代的元素,迭代得添加(分解到最后的可迭代的元素元素)
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li.extend("Math") li.extend([1,2,3]) print(li) >>>['span', [18, 188, 1888], 'python', '金融', 'FX-EXchange', 'M', 'a', 't', 'h', 1, 2, 3]
PS:int object is not iterable.
1.3
#刪除 list.pop(index) 按索引刪除
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] item = li.pop(2)#有返回值,默認是刪除最后一個 print(item,li) >>>python ['span', [18, 188, 1888], '金融', 'FX-EXchange']
#刪除 list.remove() 按元素刪除
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li.remove("span")#無返回值 print(li) >>>[[18, 188, 1888], 'python', '金融', 'FX-EXchange']
#清空一個列表 list.clear() #清空一個列表,列表仍存在
#刪除一個列表 del list
del li print(li) >>>NameError: name 'li' is not defined
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] del li[2:]#切片刪除 print(li) >>>['span', [18, 188, 1888]]
1.4 改動列表里面的值,可直接按索引賦值 如:list[index] = “”
還可以切片改:
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li[0:2] = "我愛"#iterable value print(li) >>>['我', '愛', 'python', '金融', 'FX-EXchange']
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] li[0:2] = [1,2,3,"我","愛"] print(li) >>>[1, 2, 3, '我', '愛', 'python', '金融', 'FX-EXchange']
1.5 查 #可按索引查,也可按切片查 也可以通過for循環查找所有的元素
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] for i in li: print(i) >>> span [18, 188, 1888] python 金融 FX-EXchange
注:公共方法:
列表元素的個數len();元素出現的個數:list.count(value);通過元素找索引 list.index(value)
li = ["span",[18,188,1888],"python","金融","FX-EXchange"] i = len(li) num = li.count("spam") index = li.index("python")#如果查不到會報錯 print(i,num,index) >>> 5 1 2
# 排序
list.sort()#正序排序;
list.sort(reverse=True)#倒序排序
list.reverse()#反轉
#####補充:列表的嵌套及其他操作
一、找到列表中的某個元,並顯示元素的元素
#補充:列表的嵌套 li = ["span",[18,188,1888],"python","金融","FX-EXchange"] print(li[3][0])#找到“金融”的金字,並將其打印 >>>金
#找到列表第一個元素,將其首字母大寫,並放回 li = ["span",[18,188,1888],"python","金融","FX-EXchange"] name = li[0].capitalize() li[0] = name print(li[0]) >>>Span
#找到‘金融’二字,將‘融’字替換成“元寶”
li = ["span",[18,188,1888],"python","金融","FX-EXchange"]
#li[3] = "金元寶"
#或者
li[3] = li[3].replace("融","元寶")
print(li)
>>>['span', [18, 188, 1888], 'python', '金元寶', 'FX-EXchange']
****元組被稱為只讀列表,即數據可以被查詢,但不能被修改,所以,字符串的切片操作同樣適用於元組。***
# tuple
#元組,只讀列表,可查詢可切片:兒子不能夠孫子能改 tu = ("span",[18,188,1888],["python",123,666],"金融","FX-EXchange") #將1888,改成808 tu[1][2] = 808 print(tu) >>>('span', [18, 188, 808], ['python', 123, 666], '金融', 'FX-EXchange') #在列表后面添加9999 tu[1].append(999) print(tu) >>>('span', [18, 188,
#列表轉換成字符串"".join(iterable)
###列表轉換成字符串
li = ["span","金融","FX-EXchange"] s = "".join(li) print(s) >>>span金融FX-EXchange name = "span" s = "_".join(name) print(s) >>>s_p_a_n
#打印列表中的每一個元素
li = ["span",[18,188,1888],["python",123,666],"金融","FX-EXchange"] for i in range(len(li)): if type(li[i]) == list: for j in li[i]: print(j,end=" ") else: print(li[i],end=" ") >>>span 18 188 1888 python 123 666 金融 FX-EXchange
###元組里面有一個元素且不加逗號,此元素是什么類型就是什么類型
## tu1 = (1) tu2 = (1,) print(tu1,type(tu1)) print(tu2,type(tu2)) 》》》1 <class 'int'> 》》》(1,) <class 'tuple'>
