上一篇文章: python字符串處理
下一篇文章: Python建立數值列表
實例:python
list=["my","name","is","mark","age",18] print(len(list)) list2=[] print(len(list2))
控制檯打印結果:segmentfault
6 0
list=["my","name","is","mark","age",18] for item in list: print(item)
打印結果:數組
my name is mark age 18
list=["my","name","is","mark","age",18] i=0 while i<len(list): print(list[i]) i+=1
打印結果:app
my name is mark age 18
實例:code
list=["my","name","is","mark","age",18] print("添加前:",list) list.append("test") print("添加後:",list)
打印結果:對象
添加前: ['my', 'name', 'is', 'mark', 'age', 18] 添加後: ['my', 'name', 'is', 'mark', 'age', 18, 'test']
實例:排序
list=["my","name","is","mark","age",18] print("extend前:",list) list2=["A","B"] list.extend(list2) print("extend後:",list)
打印結果:rem
extend前: ['my', 'name', 'is', 'mark', 'age', 18] extend後: ['my', 'name', 'is', 'mark', 'age', 18, 'A', 'B']
實例:字符串
list=["my","name","is","mark","age",18] print("insert前:",list) list.insert(3,"test") print("insert後:",list)
打印結果:get
insert前: ['my', 'name', 'is', 'mark', 'age', 18] insert後: ['my', 'name', 'is', 'test', 'mark', 'age', 18]
實例:
list=["my","name","is","mark","age",18] print("修改前:",list) list[len(list)-1]=19 print("修改後:",list)
結果:
修改前: ['my', 'name', 'is', 'mark', 'age', 18] 修改後: ['my', 'name', 'is', 'mark', 'age', 19]
實例:
list=["my","name","is","mark","age",18] print("mark" in list) print("Mark" in list) print("mark" not in list) print("Mark" not in list)
結果:
True False False True
語法:list.index(目標對象[,開始下標,結束下標])
實例:
list=["my","name","is","mark","age",18] print(list.index("name")) print(list.index("name",0,2)) print(list.index("name",1,3))
結果:
1 1 1
實例:
list=["my","name","is",18,"mark","age",18] print(list.count(18)) print(list.count("mark")) print(list.count(19))
結果:
2 1 0
del:根據下標刪除pop:刪除最後一個元素,至關於彈出棧頂元素,若是指定下標,也能夠刪除任意位置元素。
remove:根據元素的值進行刪除,只刪除最早找到的那個
實例:
list=["my","name",18,"is",18,"mark","age",18] print("刪除前:",list) del list[1] print("del後:",list) list=["my","name",18,"is",18,"mark","age",18] list.pop() print("pop後:",list) list=["my","name",18,"is",18,"mark","age",18] list.pop(0) print("pop(0)後:",list) list=["my","name",18,"is",18,"mark","age",18] list.remove(18) print("remove後:",list)
結果:
刪除前: ['my', 'name', 18, 'is', 18, 'mark', 'age', 18] del後: ['my', 18, 'is', 18, 'mark', 'age', 18] pop後: ['my', 'name', 18, 'is', 18, 'mark', 'age'] pop(0)後: ['name', 18, 'is', 18, 'mark', 'age', 18] remove後: ['my', 'name', 'is', 18, 'mark', 'age', 18]
sort:將數組從小到大排序,參數reverse=True可改爲從大到小排序,永久排序reverse:將數組倒置,永久排序
sorted:效果同sort,只不過是臨時排序
實例:
list=[1,3,5,2,7,8,4,0] print("排序前:",list) list.sort() print("sort後:",list) list=[1,3,5,2,7,8,4,0] list.sort(reverse=True) print("sort(reverse=True)後:",list) list=[1,3,5,2,7,8,4,0] list.reverse() print("reverse後:",list) list=[1,3,5,2,7,8,4,0] sorted(list,reverse=True) print("sorted後(臨時操做不影響原有列表):",list) list=[1,3,5,2,7,8,4,0] print("sorted後:",sorted(list,reverse=True))
結果:
排序前: [1, 3, 5, 2, 7, 8, 4, 0] sort後: [0, 1, 2, 3, 4, 5, 7, 8] sort(reverse=True)後: [8, 7, 5, 4, 3, 2, 1, 0] reverse後: [0, 4, 8, 7, 2, 5, 3, 1] sorted後(臨時操做不影響原有列表): [1, 3, 5, 2, 7, 8, 4, 0] sorted後: [8, 7, 5, 4, 3, 2, 1, 0]
實例:
list=[1,3,5,2,7,8,4,0] print("列表最小值:%d"%min(list)) print("列表最大值:%d"%max(list)) print("列表總和:%d"%sum(list))
結果:
列表最小值:0 列表最大值:8 列表總和:30