Python列表處理

Python列表處理

上一篇文章: python字符串處理
下一篇文章: Python建立數值列表

0、切片操做。同字符串切片操做,這裏再也不贅述

一、獲取列表長度:len

實例:python

list=["my","name","is","mark","age",18]
print(len(list))
list2=[]
print(len(list2))

控制檯打印結果:segmentfault

6
0

二、列表的循環遍歷

  • for循環
list=["my","name","is","mark","age",18]
for item in list:
    print(item)

打印結果:數組

my
name
is
mark
age
18
  • while循環
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

三、添加元素:append、extend、insert

  • append:向列表添加元素,添加到尾部

實例: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']
  • extend:將另一個列表的元素逐一添加到指定列表中

實例:排序

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']
  • inset(index,objectA):在指定位置index前面插入對象objectA

實例:字符串

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]

五、查找元素:in、not in、index、count

  • in、not in查找指定元素是否存在,或者不存在

實例:

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
  • index:查找指定元素是否存在,存在返回下標,不存在返回-1
語法: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
  • count:返回指定對象在列表中出現的次數

實例:

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

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、sorted

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]

八、列表最大值、最小值、總和:min、max、sum

實例:

list=[1,3,5,2,7,8,4,0]
print("列表最小值:%d"%min(list))
print("列表最大值:%d"%max(list))
print("列表總和:%d"%sum(list))

結果:

列表最小值:0
列表最大值:8
列表總和:30
相關文章
相關標籤/搜索