Python學習筆記(3)——列表的索引、切片、增刪改和排序

列表是由一系列按順序排列的元素組成,列表的元素可以是字母、數字或任何東西,它們之間沒有關係,用方括號([ ])表示列表。

>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1. 索引從0開始

與大多數編程語言一樣,列表的索引從0開始,而且還提供從右往左數。

>>> numbers[0]
1
>>> numbers[9]
10
>>> numbers[-1]
10
>>> numbers[-2]
9

2. 用切片訪問多個元素

可以指定兩個索引爲切片的邊界,訪問列表中的一段,需要注意的是第二個索引指定的元素不含在切片內。

>>> numbers[0:5]
[1, 2, 3, 4, 5]
>>> numbers[-3:-1]  # 不包含最後的元素
[8, 9]
>>> numbers[-3:]  # 這樣可以包含最後一個元素
[8, 9, 10]
>>> numbers[:5]  # 從頭開始可以省略第一個索引
[1, 2, 3, 4, 5]
>>> numbers[:]  # 兩個索引都省略就是訪問所有元素
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[2:1]  # 如果第二個索引比第一個小,返回空
[]

加上步長,就可以跳着訪問元素了,默認步長是1。

>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> numbers[0:10:3]
[1, 4, 7, 10]
>>> numbers[10:0:-2]  # 步長爲負數時,可以倒着訪問
[10, 8, 6, 4, 2]

3. 使用加法和乘法

加法運算可用來拼接序列;乘法運算可用來創建一個重複n次的新列表。

>>> numbers + [11,12,13]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> newNumbers = numbers * 2
>>> newNumbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

4. in、len、min、max、sum

檢查一個值是否包含在列表中可以用運算符in;

內置函數len、min、max、sum用於返回列表的長度、最小值、最大值和總和;

>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 2 in numbers
True
>>> 'aa' in numbers
False

>>> len(numbers)
10
>>> min(numbers)
1
>>> max(numbers)
10
>>> max(['a','b'])
'b'
>>> sum(numbers)
55

5. 利用索引和切片操作列表

利用索引可以直接修改或刪除列表的元素。

>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0] = -1
>>> numbers
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> del numbers[0]
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

也可以利用切片修改多個元素,甚至可以巧妙的插入或刪除元素。

>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[:3] = [0,0,0]  # 把前3個元素改成0
>>> numbers
[0, 0, 0, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[:3] = []  # 把前3個元素刪掉
>>> numbers
[4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:0] = [1,2,3]  # 在索引0出再插入1,2,3
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> del numbers[:3]  # 也可以用del刪除元素
>>> numbers
[4, 5, 6, 7, 8, 9, 10]

6. 列表常用方法

  • append、extend、insert

append方法用於將一個對象追加到列表末尾;

extend方法可以將多個值追加到末尾;

insert方法可以在指定位置插入元素。

>>> a = [1, 2, 3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> a.extend([5, 6, 7])
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> a.insert(3, 3.5)
>>> a
[1, 2, 3, 3.5, 4, 5, 6, 7]
  • pop、remove、clear

pop方法從列表中刪除一個元素(默認爲最後一個元素),並返回被刪除的元素; 

remove方法的參數是一個指定值,用於刪除列表中第一個該指定值的元素;

clear方法清空列表。

>>> a = [1, 2, 3]
>>> a.pop()
3
>>> a
[1, 2]
>>> a.pop(0)
1
>>> a
[2]
>>> b = [1, 2, 3, 3, 2, 1]
>>> b.remove(2)
>>> b
[1, 3, 3, 2, 1]
>>> b.clear()
>>> b
[]
  • count、index

count方法用於計算指定元素在列表中出現的次數;

index方法用於查找指定值第一次出現的位置,找不到的話就會報錯。

>>> a = [1, 2, 3] * 2
>>> a
[1, 2, 3, 1, 2, 3]
>>> a.count(1)
2
>>> a.index(3)
2

  • copy

copy方法可以複製列表,也可以說是創建一個副本,也可以用a[:]或list(a)的方式來複制a,但b=a卻不能複製a,而是將另一個名稱關聯到li列表。

>>> a = [1, 2, 3]
>>> b = a  # a和b指向同一個列表
>>> b[0] = -1
>>> a
[-1, 2, 3]
>>> 
>>> a = [1, 2, 3]
>>> # 使用copy方法創建一個副本
>>> b = a.copy()
>>> b[0] = -1
>>> b
[-1, 2, 3]
>>> a
[1, 2, 3]
>>> # 使用a[:]創建一個副本
>>> c = a[:]
>>> c[0] = -1
>>> c
[-1, 2, 3]
>>> a
[1, 2, 3]
>>> # 使用list(a)創建一個副本
>>> d = list(a)
>>> d[0] = -1
>>> d
[-1, 2, 3]
>>> a
[1, 2, 3]
  • reverse、sort、sorted

reverse方法將列表逆序排列,直接修改原列表,但不返回值;

sort方法對列表中的元素按順序排序,同樣是xiug修改原列表,不返回值;

sorted方法對列表排序並返回一個排序後的副本,不修改原列表。

>>> a = [1 ,5, 2, 9, 8, 10]
>>> a.reverse()
>>> a
[10, 8, 9, 2, 5, 1]
>>> a = [1 ,5, 2, 9, 8, 10]
>>> a.sort()
>>> a
[1, 2, 5, 8, 9, 10]
>>> a = [1 ,5, 2, 9, 8, 10]
>>> b = sorted(a)
>>> b
[1, 2, 5, 8, 9, 10]
>>> a
[1, 5, 2, 9, 8, 10]

sort方法有兩個可選參數:key和reverse。參數key的值可以是一個用於排序的函數(可以是自定義函數),該函數爲每個元素創建一個鍵,在根據這些鍵值來排序。reverse默認值爲False,設爲True時則逆序排列。

>>> a = ['hello', 'python', 'a', 'no']
>>> a.sort(key=len)
>>> a
['a', 'no', 'hello', 'python']
>>> a.sort(key=len, reverse=True)
>>> a
['python', 'hello', 'no', 'a']

7. 元組

如果想創建一系列不可修改的元素,可以使用元組。定義一個元組的方式是用逗號分隔元素,也可以用圓括號括起來。使用tuple方法可將列表轉換爲元組。

>>> a = 1,2,3
>>> a
(1, 2, 3)
>>> b = (1,2,3)
>>> b
(1, 2, 3)
>>> c = 1,  # 創建只含一個元素的元組,必須有逗號
>>> c
(1,)
>>> d = tuple([3,2,1])
>>> d
(3, 2, 1)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章