Python3 基础学习笔记 C02【列表】
CSDN 課程推薦:《8小時Python零基礎輕松入門》,講師齊偉,蘇州研途教育科技有限公司CTO,蘇州大學應用統計專業碩士生指導委員會委員;已出版《跟老齊學Python:輕松入門》《跟老齊學Python:Django實戰》、《跟老齊學Python:數據分析》和《Python大學實用教程》暢銷圖書。
Python3 基礎學習筆記第二章【列表】
目錄
- 【2.1】列表是什么
- 【2.1.1】訪問列表元素
- 【2.1.2】列表切片
- 【2.1.3】使用列表中的各個值
- 【2.1.4】修改元素
- 【2.1.5】添加元素
- 【2.1.6】刪除元素
- 【2.1.7】使用方法 index() 查找指定元素位置
- 【2.1.8】使用方法 count() 統計指定元素數量
- 【2.1.9】清空列表
- 【2.2】組織列表
- 【2.2.1】使用方法 sort() 對列表進行永久排序
- 【2.2.2】使用函數 sorted() 對列表進行臨時排序
- 【2.2.3】使用方法 reverse() 對列表進行反向排序
- 【2.2.4】確定列表的長度
- 【2.2.5】合并列表
【2.1】列表是什么
列表由一系列按特定順序的元素組成,在 Python 中用方括號( [ ] )來表示列表,并用逗號來分隔其中的元素,例:
list1 = ['a','b','c','d','e','f'] list2 = ['abc', 'xyz', 2018, 2020] list3 = [1, 2, 3, 4, 5 ,6] list4 = ["a", "b", "c", "d"] print(list1, list2, list3 ,list4)輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['abc', 'xyz', 2018, 2020] [1, 2, 3, 4, 5, 6] ['a', 'b', 'c', 'd']【2.1.1】訪問列表元素
列表是有序集合,因此要訪問列表的元素,只需要將該元素的位置或索引告訴Python即可,注意:在Python中的第一個列表元素的索引為0,而不是1
list = ['a','b','c','d','e','f'] print(list[0]) print(list[3]) print(list[-1]) #Python為訪問最后一個列表元素提供了一種特殊語法,通過將索引指定為-1,可以讓Python返回最后一個列表元素 print(list[-3])輸出結果如下:
a d f d【2.1.2】列表切片
list = ['a','b','c','d','e','f'] print(list[:]) #省略全部,代表截取全部內容,可以用來將一個列表拷給另一個列表 print(list[:3]) #省略起始位置的索引,默認起始位置從頭開始,結束位置索引為2 print(list[3:]) #省略結束位置的索引,默認結束位置為最后一個,開始位置索引為3 print(list[1:4]) #開始位置索引為1,結束位置索引為3,顧頭不顧尾 print(list[4:1]) #從左到右索引,因此為空值 print(list[-1:-3]) #從左到右索引,因此為空值 print(list[-3:-1]) #開始位置索引為倒數第三個,結束位置索引為倒數第二個 print(list[1:5:2]) #開始位置索引為1,結束位置索引為4,間隔2 print(list[5:1:-1]) #反向取值,開始位置索引為5,結束位置索引為2 print(list[::-1]) #反向取值,反向輸出列表輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['a', 'b', 'c'] ['d', 'e', 'f'] ['b', 'c', 'd'] [] [] ['d', 'e'] ['b', 'd'] ['f', 'e', 'd', 'c'] ['f', 'e', 'd', 'c', 'b', 'a']【2.1.3】使用列表中的各個值
可像使用其他變量一樣使用列表中的各個值,例如,我們可以使用拼接根據列表中的值來創建消息:
list = ['python', 'c', 'c++', 'java', 'php'] message = "My favorite language is " + list[0].title() + "!" print(message)輸出結果如下:
My favorite language is Python!【2.1.4】修改元素
修改列表元素的語法與訪問列表元素的語法類似,要修改列表元素,可指定列表名和要修改的元素的索引,再次指定該元素的新值:
names = ['zhangsan', 'lishi', 'wanger', 'liming', 'xiaowang'] print(names) names[1] = 'lifang' print(names)輸出結果如下:
['zhangsan', 'lishi', 'wanger', 'liming', 'xiaowang'] ['zhangsan', 'lifang', 'wanger', 'liming', 'xiaowang']【2.1.5】添加元素
- 使用方法 append() 在列表末尾添加元素
list = ['a', 'b', 'c', 'd', 'e', 'f'] print(list) list.append('g') print(list)輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['a', 'b', 'c', 'd', 'e', 'f', 'g']- 使用方法 insert() 在列表指定位置添加元素
list = ['a', 'b', 'c', 'd', 'e', 'f'] print(list) list.insert(2,"h") #其中括號里的數字表示要插入的位置,此后面的元素將右移一個位置 print(list)輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g']【2.1.6】刪除元素
- 使用 del 語句刪除元素
list = ['a', 'b', 'c', 'd', 'e', 'f'] print(list) del list[3] print(list)輸出結果如下:
list = ['a', 'b', 'c', 'd', 'e', 'f'] list = ['a', 'b', 'c', 'e', 'f'] - 使用方法 pop() 刪除最后一個元素
方法 pop() 可以刪除列表末尾的元素,并讓你能夠接著使用它。術語彈出(pop)源自這樣的類比:列表就像是一個棧,而刪除列表末尾的元素就相當于彈出棧頂元素:
輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['a', 'b', 'c', 'd', 'e'] f - 使用方法 pop() 刪除任意位置元素
可以使用 pop() 來刪除列表中任何位置的元素,只需要在括號中指定要刪除的元素的索引即可:
輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['a', 'c', 'd', 'e', 'f'] b - 使用方法 remove() 刪除未知位置元素
當我們不知道元素的位置,只知道元素的值的時候,就可以使用方法 remove()
輸出結果如下:
['a', 'b', 'c', 'd', 'e', 'f'] ['a', 'b', 'c', 'e', 'f']【2.1.7】使用方法 index() 查找指定元素位置
list = ["a", "b", "c", "d", "e", "a"] print(list.index('c'))輸出結果如下:
2【2.1.8】使用方法 count() 統計指定元素數量
list = ["a", "b", "c", "d", "e", "a"] print(list.count('a'))輸出結果如下:
2【2.1.9】清空列表
list = ["a", "b", "c", "d", "e", "a"] list.clear() print(list)輸出結果如下:
[]【2.2】組織列表
在創建的列表中,元素的排列順序常常是無法預測的,因為我們并非總能控制用戶提供數據的順序。有時候,我們希望保留列表元素最初的排列順序,而有時候又需要調整排列順序。Python提供了很多組織列表的方式,可根據具體情況選用
【2.2.1】使用方法 sort() 對列表進行永久排序
使用方法 sort() 可以對列表按照特殊符號,數字,大寫字母,小寫字母順序進行永久排序:
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars)輸出結果如下:
['audi', 'bmw', 'subaru', 'toyota']還可以按與字母順序相反的順序排列列表元素,只需要向 sort() 方法傳遞參數 reverse = True 就可以了:
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort(reverse = True) print(cars)輸出結果如下:
['toyota', 'subaru', 'bmw', 'audi']【2.2.2】使用函數 sorted() 對列表進行臨時排序
要保留列表元素原來的排列順序,同時以特定的順序呈現它們,可使用函數sorted()。函數sorted()讓你能夠按特定順序顯示列表元素,同時不影響它們在列表中的原始排列順序:
cars = ['bmw', 'audi', 'toyota', 'subaru'] print("Here is the original list:") print(cars) print("\nHere is the sorted list:") print(sorted(cars)) print("\nHere is the sorted reverse list:") print(sorted(cars, reverse=True)) print("\nHere is the original list again:") print(cars)輸出結果如下:
Here is the original list: ['bmw', 'audi', 'toyota', 'subaru']Here is the sorted list: ['audi', 'bmw', 'subaru', 'toyota']Here is the sorted reverse list: ['toyota', 'subaru', 'bmw', 'audi']Here is the original list again: ['bmw', 'audi', 'toyota', 'subaru']【2.2.3】使用方法 reverse() 對列表進行反向排序
要反轉列表元素的排列順序,可使用方法 reverse()
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.reverse() print(cars)輸出結果如下:
['subaru', 'toyota', 'audi', 'bmw']【2.2.4】確定列表的長度
使用函數 len() 可以快速獲悉列表的長度:
>>>cars = ['bmw', 'audi', 'toyota', 'subaru'] >>>len(cars) 4【2.2.5】合并列表
- 使用方法 extend() 合并列表
list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] list1.extend(list2) #將列表list2添加到list1當中去 print(list1) print(list2)輸出結果如下:
[1, 2, 3, 4, 'a', 'b', 'c', 'd'] ['a', 'b', 'c', 'd']- 使用 “+” 號合并列表
list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] print(list1 + list2) print(list2 + list1)輸出結果如下:
[1, 2, 3, 4, 'a', 'b', 'c', 'd'] ['a', 'b', 'c', 'd', 1, 2, 3, 4]- 使用切片合并列表
list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] list1[len(list1) : len(list1)] = list2 #len(list1)代表要將list2插入list1中的位置 print(list1)list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] list1[0 :0] = list2 print(list1)list1 = [1, 2, 3, 4] list2 = ['a', 'b', 'c', 'd'] list1[1:1] = list2 print(list1)輸出結果如下:
[1, 2, 3, 4, 'a', 'b', 'c', 'd'] ['a', 'b', 'c', 'd', 1, 2, 3, 4] [1, 'a', 'b', 'c', 'd', 2, 3, 4]總結
以上是生活随笔為你收集整理的Python3 基础学习笔记 C02【列表】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鼻子上出的油都是哪来的?怪它 也怪你
- 下一篇: volatile学习(可见性,不保证原子