## 3.1推導式練習
給定一個包含5個英語單詞的列表,請構造1個字典以列表的元素為鍵,以每個鍵對應的單詞字母個數為值。
~~~
en_list = ["apple", "banana", "peach", "pineapple", "watermelon"]
~~~
~~~
en_dict = {word: len(word) for word in en_list}
en_dict
~~~
## 3.4 練習:for循環和控制臺打印
```
# 假設有三名學生
students = ["小明", "小紅", "小剛"]
# 每位學生的分數
scores = [90, 85, 78]
# 開始打印報告卡
print("學生分數報告卡:")
# 對于每一個學生,進行一次循環
for i in range(len(students)):
# 獲取學生的名字和分數
student_name = students[i]
score = scores[i]
# 打印學生的名字和分數
print(f"{student_name}的分數是:{score}分")
# 結束打印報告卡
print("報告卡打印完畢!")
```
## 3.6 列表練習:計算銷售額
```
# 假設每天售出的商品和數量如下
sales = [
{"product": "蘋果", "quantity": 10, "price": 5},
{"product": "香蕉", "quantity": 8, "price": 3},
{"product": "橙子", "quantity": 12, "price": 4},
]
# 計算總銷售額
total_sales = 0
# 對于每一天的銷售記錄,進行一次循環
for sale in sales:
# 獲取商品的數量和價格
quantity = sale["quantity"]
price = sale["price"]
# 計算當天的銷售額
daily_sales = quantity * price
# 累加到總銷售額
total_sales += daily_sales
# 打印當天的銷售額
print(f"{sale['product']}的銷售額是:{daily_sales}元")
# 打印總銷售額
print("總銷售額是:", total_sales, "元")
```
## 3.7 列表的切片的練習
```
# 食材列表
food_list = ["蔥", "姜", "蒜", "料酒", "豬肝", "大閘蟹", "雞丁", "花生米", "醬油", "淀粉"]
# 獲取所需食材
selected_food1 = food_list[0:4]
selected_food2 = food_list[-4:]
select_list = selected_food1+selected_food2
# 打印選中的食材
print("您選中的食材是:")
for food in select_list:
print(food)
```
## 3.8列表組成字典
```
list_food=["腰子","辣子","和牛","大閘蟹","黑虎蝦","西蘭花"]
list_price=[68,0.5,128,60,40,12]
list = {}
for food,price in zip(list_food,list_price):
list[food] = price
list
```
## 3.8 zip函數練習
```
# 步驟 1: 創建一個包含客人名字和喜好的字典
guests = [
"老王",
"老馬",
"老陳",
"李小寶",
"小林"
]
likes=[
"川菜",
"西式快餐",
"南通菜",
"東北菜",
"法餐"
]
feeds = [
"缽缽雞",
"巨無霸漢堡",
"南通頭菜",
"鍋包肉",
"紅酒鵝肝"
]
# 步驟 3: 使用 zip 函數將客人和他們的喜好配對,并打印出來
preferences = list(zip(guests, likes))
print("客人的喜好:")
for guest, preference in preferences:
print(f"{guest} 喜歡 {preference}")
# 步驟 4: 使用 zip 函數將客人和菜品配對,并創建一個新的字典
feeds_for_guests = list(zip(guests, gifts))
# 步驟 5: 打印新的字典,查看每個客人將收到的禮物
print("\n客人點的菜:")
for guest, feeds in feeds_for_guests:
print(f"{guest} 點了 {feeds}")
```
## 3.8 字典——練習
```
# 創建一個空的菜單字典
menu = {
"宮保雞丁": 35.0,
"麻婆豆腐": 28.0,
"清蒸魚": 45.0,
"炒時蔬": 20.0,
"米飯": 5.0
}
# 創建一個空的訂單字典
order = {}
def add_to_order(dish_name):
"""將菜品添加到訂單中"""
if dish_name in menu:
if dish_name in order:
order[dish_name] += 1
else:
order[dish_name] = 1
else:
print("該菜品不在菜單中。")
def remove_from_order(dish_name):
"""從訂單中刪除菜品"""
if dish_name in order:
if order[dish_name] > 1:
order[dish_name] -= 1
else:
del order[dish_name]
else:
print("訂單中不存在該菜品。")
def calculate_total():
"""計算訂單的總價"""
total = sum(menu[dish] * quantity for dish, quantity in order.items())
return total
def view_order():
"""查看訂單詳情"""
if order:
print("訂單詳情:")
for dish, quantity in order.items():
print(f"{dish}: {quantity} 份")
print(f"總價: {calculate_total()} 元")
else:
print("訂單為空。")
# 示例使用
add_to_order("宮保雞丁")
add_to_order("清蒸魚")
add_to_order("炒時蔬")
view_order()
remove_from_order("清蒸魚")
view_order()
print(f"最終訂單總價: {calculate_total()} 元")
```
## 3.9 集合的練習
```
# 你的購物清單
your_list = {"烤鴨", "籃球鞋", "iphone15", "牛肉卷", "提拉米蘇"}
# 朋友的購物清單
friend_list = {"烤鴨", "牛奶", "雞蛋", "牛肉卷"}
# 使用并集操作合并清單
combined_list = your_list.union(friend_list)
print("合并后的購物清單:", combined_list)
# 使用交集操作找出共同想購買的物品
common_items = your_list.intersection(friend_list)
print("共同想購買的物品:", common_items)
```
## 項目2


