利用 Python 列表和字典實(shí)現(xiàn)簡單的數(shù)據(jù)庫
在今天的教程中,我們將學(xué)習(xí)如何使用 Python 的列表和字典來實(shí)現(xiàn)一個(gè)簡單的數(shù)據(jù)庫。通過這個(gè)過程,你會了解到如何存儲、檢索和操作數(shù)據(jù)。讓我們一步步來,從基礎(chǔ)到高級,確保每一個(gè)步驟都能輕松掌握。
1. 列表和字典的基礎(chǔ)
首先,我們需要了解 Python 中的列表和字典是什么,以及它們的基本用法。
(1) 列表
列表是 Python 中的一種有序集合,可以存儲多個(gè)項(xiàng)目。創(chuàng)建一個(gè)列表非常簡單:
# 創(chuàng)建一個(gè)列表
my_list = [1, 2, 3, 4, 5]
print(my_list) # 輸出: [1, 2, 3, 4, 5]
(2) 字典
字典是一種無序的鍵值對集合。每個(gè)鍵值對由一個(gè)鍵和一個(gè)值組成。創(chuàng)建一個(gè)字典也很簡單:
# 創(chuàng)建一個(gè)字典
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(my_dict) # 輸出: {'name': 'Alice', 'age': 25, 'city': 'New York'}
2. 使用列表和字典存儲數(shù)據(jù)
假設(shè)我們要存儲一些用戶信息,包括用戶名、年齡和城市。我們可以使用一個(gè)字典來表示每個(gè)用戶,然后將這些字典存儲在一個(gè)列表中。
# 創(chuàng)建一個(gè)用戶字典
user1 = {'username': 'alice', 'age': 25, 'city': 'New York'}
user2 = {'username': 'bob', 'age': 30, 'city': 'Los Angeles'}
# 將用戶字典存儲在列表中
users = [user1, user2]
# 打印用戶列表
print(users)
# 輸出: [{'username': 'alice', 'age': 25, 'city': 'New York'}, {'username': 'bob', 'age': 30, 'city': 'Los Angeles'}]
3. 檢索數(shù)據(jù)
現(xiàn)在我們已經(jīng)存儲了一些用戶數(shù)據(jù),接下來學(xué)習(xí)如何檢索這些數(shù)據(jù)。
(1) 按索引檢索
我們可以使用列表的索引來訪問特定的用戶:
# 獲取第一個(gè)用戶
first_user = users[0]
print(first_user) # 輸出: {'username': 'alice', 'age': 25, 'city': 'New York'}
(2) 按鍵檢索
我們還可以使用字典的鍵來訪問特定的信息:
# 獲取第一個(gè)用戶的年齡
first_user_age = first_user['age']
print(first_user_age) # 輸出: 25
4. 添加和刪除數(shù)據(jù)
(1) 添加數(shù)據(jù)
我們可以使用 append 方法將新用戶添加到列表中:
# 創(chuàng)建一個(gè)新的用戶
new_user = {'username': 'charlie', 'age': 22, 'city': 'Chicago'}
# 將新用戶添加到列表中
users.append(new_user)
# 打印更新后的用戶列表
print(users)
# 輸出: [{'username': 'alice', 'age': 25, 'city': 'New York'}, {'username': 'bob', 'age': 30, 'city': 'Los Angeles'}, {'username': 'charlie', 'age': 22, 'city': 'Chicago'}]
(2) 刪除數(shù)據(jù)
我們可以使用 remove 方法或 pop 方法來刪除用戶:
# 刪除第一個(gè)用戶
users.remove(user1)
# 打印更新后的用戶列表
print(users)
# 輸出: [{'username': 'bob', 'age': 30, 'city': 'Los Angeles'}, {'username': 'charlie', 'age': 22, 'city': 'Chicago'}]
5. 更新數(shù)據(jù)
我們可以直接修改字典中的值來更新用戶信息:
# 更新第二個(gè)用戶的年齡
users[1]['age'] = 23
# 打印更新后的用戶列表
print(users)
# 輸出: [{'username': 'bob', 'age': 23, 'city': 'Los Angeles'}, {'username': 'charlie', 'age': 22, 'city': 'Chicago'}]
6. 高級操作:查詢和過濾
我們可以使用列表推導(dǎo)式和條件語句來查詢和過濾數(shù)據(jù)。
查詢特定城市的用戶:
# 查詢所有住在 Los Angeles 的用戶
los_angeles_users = [user for user in users if user['city'] == 'Los Angeles']
# 打印結(jié)果
print(los_angeles_users)
# 輸出: [{'username': 'bob', 'age': 23, 'city': 'Los Angeles'}]
過濾年齡大于 22 的用戶:
# 過濾年齡大于 22 的用戶
older_users = [user for user in users if user['age'] > 22]
# 打印結(jié)果
print(older_users)
# 輸出: [{'username': 'bob', 'age': 23, 'city': 'Los Angeles'}]
7. 實(shí)戰(zhàn)案例:用戶管理系統(tǒng)
假設(shè)我們要實(shí)現(xiàn)一個(gè)簡單的用戶管理系統(tǒng),支持添加、刪除、更新和查詢用戶信息。
# 初始化用戶列表
users = []
def add_user(username, age, city):
"""添加新用戶"""
new_user = {'username': username, 'age': age, 'city': city}
users.append(new_user)
print(f"用戶 {username} 已添加")
def remove_user(username):
"""刪除用戶"""
global users
users = [user for user in users if user['username'] != username]
print(f"用戶 {username} 已刪除")
def update_user(username, age=None, city=None):
"""更新用戶信息"""
for user in users:
if user['username'] == username:
if age is not None:
user['age'] = age
if city is not None:
user['city'] = city
print(f"用戶 {username} 的信息已更新")
return
print(f"未找到用戶 {username}")
def query_users(city=None, age=None):
"""查詢用戶"""
if city and age:
result = [user for user in users if user['city'] == city and user['age'] == age]
elif city:
result = [user for user in users if user['city'] == city]
elif age:
result = [user for user in users if user['age'] == age]
else:
result = users
return result
# 測試用戶管理系統(tǒng)的功能
add_user('alice', 25, 'New York')
add_user('bob', 30, 'Los Angeles')
add_user('charlie', 22, 'Chicago')
print(query_users()) # 輸出所有用戶
print(query_users(city='Los Angeles')) # 查詢 Los Angeles 的用戶
print(query_users(age=22)) # 查詢年齡為 22 的用戶
update_user('alice', age=26)
remove_user('bob')
print(query_users()) # 輸出更新后的用戶列表
總結(jié)
在本文中,我們學(xué)習(xí)了如何使用 Python 的列表和字典來實(shí)現(xiàn)一個(gè)簡單的數(shù)據(jù)庫。我們從基礎(chǔ)的列表和字典創(chuàng)建開始,逐步介紹了如何存儲、檢索、添加、刪除和更新數(shù)據(jù)。最后,我們通過一個(gè)實(shí)戰(zhàn)案例實(shí)現(xiàn)了用戶管理系統(tǒng),展示了這些技術(shù)的實(shí)際應(yīng)用。