Python數據結構:掌握列表、字典和集合
學習目標
通過本課程的學習,學員將掌握Python中基本的數據結構:列表、字典和集合。學員將了解它們的特性、使用場景以及如何高效地使用它們來解決實際問題。
相關知識點
列表、字典和集合使用
學習內容
1 列表、字典和集合使用
1.1 列表(List)的使用
列表是Python中最常用的數據結構之一,它是一個有序的元素集合,可以存儲不同類型的元素。列表是可變的,這意味著可以在列表中添加、刪除或修改元素。
1.1.1 列表的基本操作
- 創建列表:可以使用方括號[]來創建列表。
- 訪問元素:通過索引來訪問列表中的元素,索引從0開始。
- 修改元素:通過索引可以修改列表中的元素。
- 添加元素:可以使用append()方法在列表末尾添加元素,或者使用insert()方法在指定位置插入元素。
- 刪除元素:可以使用remove()方法刪除指定元素,或者使用pop()方法刪除指定位置的元素。
# 創建列表
fruits = ['apple', 'banana', 'cherry']# 訪問元素
print(fruits[0]) # 輸出: apple# 修改元素
fruits[1] = 'orange'
print(fruits) # 輸出: ['apple', 'orange', 'cherry']# 添加元素
fruits.append('grape')
print(fruits) # 輸出: ['apple', 'orange', 'cherry', 'grape']# 插入元素
fruits.insert(1, 'kiwi')
print(fruits) # 輸出: ['apple', 'kiwi', 'orange', 'cherry', 'grape']# 刪除元素
fruits.remove('orange')
print(fruits) # 輸出: ['apple', 'kiwi', 'cherry', 'grape']# 刪除指定位置的元素
fruits.pop(1)
print(fruits) # 輸出: ['apple', 'cherry', 'grape']
1.1.2 列表的高級操作
- 切片:可以使用切片來獲取列表的一部分。
- 排序:可以使用sort()方法對列表進行排序。
- 反轉:可以使用reverse()方法反轉列表。
# 切片
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4]) # 輸出: [1, 2, 3]# 排序
numbers.sort()
print(numbers) # 輸出: [0, 1, 2, 3, 4, 5]# 反轉
numbers.reverse()
print(numbers) # 輸出: [5, 4, 3, 2, 1, 0]
1.2 字典(Dictionary)的操作
字典是Python中另一種常用的數據結構,它是一個無序的鍵值對集合。字典中的鍵必須是不可變類型,如字符串或數字,而值可以是任何類型。
1.2.1 字典的基本操作
- 創建字典:可以使用花括號{}來創建字典。
- 訪問元素:通過鍵來訪問字典中的值。
- 修改元素:通過鍵可以修改字典中的值。
- 添加元素:可以添加新的鍵值對。
- 刪除元素:可以使用del關鍵字刪除指定的鍵值對,或者使用pop()方法刪除并返回指定鍵的值。
# 創建字典
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}# 訪問元素
print(person['name']) # 輸出: Alice# 修改元素
person['age'] = 26
print(person) # 輸出: {'name': 'Alice', 'age': 26, 'city': 'New York'}# 添加元素
person['email'] = 'alice@example.com'
print(person) # 輸出: {'name': 'Alice', 'age': 26, 'city': 'New York', 'email': 'alice@example.com'}# 刪除元素
del person['city']
print(person) # 輸出: {'name': 'Alice', 'age': 26, 'email': 'alice@example.com'}# 刪除并返回指定鍵的值
email = person.pop('email')
print(email) # 輸出: alice@example.com
print(person) # 輸出: {'name': 'Alice', 'age': 26}
1.2.2 字典的高級操作
- 遍歷字典:可以使用for循環遍歷字典的鍵、值或鍵值對。
- 字典推導式:可以使用字典推導式來創建字典。
# 遍歷字典
for key in person:print(key, person[key])
# 輸出:
# name Alice
# age 26# 遍歷字典的鍵
for key in person.keys():print(key)
# 輸出:
# name
# age# 遍歷字典的值
for value in person.values():print(value)
# 輸出:
# Alice
# 26# 遍歷字典的鍵值對
for key, value in person.items():print(key, value)
# 輸出:
# name Alice
# age 26# 字典推導式
squares = {x: x**2 for x in range(5)}
print(squares) # 輸出: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
1.3 集合(Set)的特性
集合是Python中另一種無序且不重復的元素集合。集合中的元素必須是不可變類型,如字符串或數字。
1.3.1 集合的基本操作
- 創建集合:可以使用花括號{}來創建集合,或者使用set()函數。
- 添加元素:可以使用add()方法添加元素。
- 刪除元素:可以使用remove()方法刪除指定元素,或者使用discard()方法刪除指定元素(如果元素不存在不會報錯)。
- 集合操作:可以使用集合的交集、并集、差集等操作。
# 創建集合
fruits = {'apple', 'banana', 'cherry'}# 添加元素
fruits.add('orange')
print(fruits) # 輸出: {'apple', 'banana', 'cherry', 'orange'}# 刪除元素
fruits.remove('banana')
print(fruits) # 輸出: {'apple', 'cherry', 'orange'}# 刪除元素(如果元素不存在不會報錯)
fruits.discard('banana')
print(fruits) # 輸出: {'apple', 'cherry', 'orange'}# 集合操作
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}# 交集
intersection = set1 & set2
print(intersection) # 輸出: {3, 4}# 并集
union = set1 | set2
print(union) # 輸出: {1, 2, 3, 4, 5, 6}# 差集
difference = set1 - set2
print(difference) # 輸出: {1, 2}
1.3.2 集合的高級操作
- 集合推導式:可以使用集合推導式來創建集合。
- 集合的子集和超集:可以使用issubset()和issuperset()方法來判斷集合的子集和超集關系。
# 集合推導式
squares = {x**2 for x in range(5)}
print(squares) # 輸出: {0, 1, 4, 9, 16}# 集合的子集和超集
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}# 判斷子集
print(set1.issubset(set2)) # 輸出: True# 判斷超集
print(set2.issuperset(set1)) # 輸出: True
系列課程名列表
1. Python入門與基礎語法實踐
2. Python入門:環境搭建與基礎配置
3. Python基礎語法與編程入門
4. Python基礎數據類型入門
5. Python基礎運算符與表達式入門
6. Python條件語句入門:掌握if, else, 和elif
7. Python循環結構基礎:for與while循環的使用
8. Python函數編程入門
9. Python模塊與包入門實踐
10. Python文件處理入門
11. Python異常處理入門
12. Python面向對象編程入門
13. Python基礎數據結構:列表、字典和集合的高效使用
14. Python高級特性入門:列表推導式、生成器表達式、裝飾器和上下文管理器
15. Python入門:構建天氣查詢器