实际应用
现在你已经学习了不同的技术来查找 Python 列表中前 N 个元素,让我们探索一些实际应用。在这一步中,你将创建一个更全面的脚本,将这些概念应用于实际场景。
在项目目录中创建一个名为 practical_applications.py 的新文件,并添加以下代码:
## 查找前 N 个元素的实际应用
import heapq
from datetime import datetime
print("查找 Python 列表中前 N 个元素的实际应用\n")
## 应用 1:电子商务 - 分析产品销售额
print("应用 1:电子商务 - 分析产品销售额")
print("==================================================")
## 样本产品销售数据(product_id,product_name,units_sold,price)
product_sales = [
(101, "Smartphone X", 1250, 899.99),
(102, "Wireless Earbuds", 2100, 129.99),
(103, "Laptop Pro", 890, 1299.99),
(104, "Smart Watch", 1450, 249.99),
(105, "Tablet Mini", 1050, 399.99),
(106, "Bluetooth Speaker", 1750, 79.99),
(107, "Gaming Console", 780, 499.99),
(108, "Digital Camera", 550, 349.99),
(109, "Power Bank", 1900, 49.99),
(110, "Fitness Tracker", 1350, 129.99)
]
## 按销量查找前 3 名产品
top_sold_products = heapq.nlargest(3, product_sales, key=lambda x: x[2])
print("\n销量前 3 名的产品(按销量):")
for product in top_sold_products:
print(f" {product[1]}:售出 {product[2]} 件,单价 ${product[3]}")
## 按收入(units_sold * price)查找前 3 名产品
top_revenue_products = heapq.nlargest(3, product_sales, key=lambda x: x[2] * x[3])
print("\n收入前 3 名的产品:")
for product in top_revenue_products:
revenue = product[2] * product[3]
print(f" {product[1]}:${revenue:,.2f} 收入({product[2]} 件,单价 ${product[3]})")
## 应用 2:数据分析 - 温度监测
print("\n\n应用 2:数据分析 - 温度监测")
print("====================================================")
## 样本温度数据(日期,城市,温度)
temperature_data = [
("2023-06-15", "New York", 32.5),
("2023-06-15", "Los Angeles", 28.3),
("2023-06-15", "Chicago", 30.1),
("2023-06-15", "Houston", 35.7),
("2023-06-15", "Phoenix", 40.2),
("2023-06-15", "Miami", 33.8),
("2023-06-15", "Denver", 29.6),
("2023-06-15", "Seattle", 22.4),
("2023-06-15", "Boston", 27.9),
("2023-06-15", "Atlanta", 31.5)
]
## 查找温度最高的城市
hottest_cities = heapq.nlargest(3, temperature_data, key=lambda x: x[2])
print("\n前 3 个最热的城市:")
for city_data in hottest_cities:
print(f" {city_data[1]}:{city_data[2]}°C")
## 查找温度最低的城市
coldest_cities = heapq.nsmallest(3, temperature_data, key=lambda x: x[2])
print("\n前 3 个最冷的城市:")
for city_data in coldest_cities:
print(f" {city_data[1]}:{city_data[2]}°C")
## 应用 3:社交媒体 - 用户参与度
print("\n\n应用 3:社交媒体 - 用户参与度")
print("=============================================")
## 样本社交媒体帖子数据(post_id,title,likes,comments,shares,timestamp)
posts = [
(1001, "Breaking News: Major Announcement", 3500, 420, 1200, datetime(2023, 6, 10, 12, 30)),
(1002, "Product Review: Latest Gadget", 2200, 380, 900, datetime(2023, 6, 11, 15, 45)),
(1003, "Tutorial: Python Programming", 1800, 650, 750, datetime(2023, 6, 12, 9, 15)),
(1004, "Travel Tips for Summer Vacation", 2700, 320, 1100, datetime(2023, 6, 13, 18, 20)),
(1005, "Recipe: Delicious Desserts", 3100, 450, 1500, datetime(2023, 6, 14, 11, 10)),
(1006, "Interview with Celebrity", 4200, 580, 2200, datetime(2023, 6, 15, 14, 25)),
(1007, "Health and Fitness Guide", 1500, 280, 600, datetime(2023, 6, 16, 8, 40)),
(1008, "Movie Review: Latest Blockbuster", 2900, 410, 950, datetime(2023, 6, 17, 20, 30)),
(1009, "Tech News: Industry Updates", 2000, 300, 800, datetime(2023, 6, 18, 13, 15)),
(1010, "DIY Home Improvement Projects", 1700, 520, 700, datetime(2023, 6, 19, 16, 50))
]
## 定义一个函数来计算参与度得分(点赞、评论、分享的加权总和)
def engagement_score(post):
return post[2] + (post[3] * 2) + (post[4] * 3) ## 点赞 + (评论 * 2) + (分享 * 3)
## 按参与度得分查找前 3 名帖子
top_engaging_posts = heapq.nlargest(3, posts, key=engagement_score)
print("\n参与度最高的前 3 名帖子:")
for post in top_engaging_posts:
score = engagement_score(post)
print(f" 帖子 ID:{post[0]}")
print(f" 标题:{post[1]}")
print(f" 参与度得分:{score}")
print(f" (点赞:{post[2]},评论:{post[3]},分享:{post[4]})")
print(f" 发布于:{post[5].strftime('%Y-%m-%d %H:%M')}")
print()
## 按点赞数查找前 3 名帖子
top_liked_posts = heapq.nlargest(3, posts, key=lambda x: x[2])
print("点赞数最高的前 3 名帖子:")
for post in top_liked_posts:
print(f" {post[1]}:{post[2]} 个点赞")
## 按评论数查找前 3 名帖子
top_commented_posts = heapq.nlargest(3, posts, key=lambda x: x[3])
print("\n评论数最高的前 3 名帖子:")
for post in top_commented_posts:
print(f" {post[1]}:{post[3]} 条评论")
运行脚本以查看这些实际应用:
python3 practical_applications.py
你应该看到详细的输出,展示了如何将你学到的技术应用于实际场景:
查找 Python 列表中前 N 个元素的实际应用
应用 1:电子商务 - 分析产品销售额
==================================================
销量前 3 名的产品(按销量):
Wireless Earbuds:售出 2100 件,单价 $129.99
Power Bank:售出 1900 件,单价 $49.99
Bluetooth Speaker:售出 1750 件,单价 $79.99
收入前 3 名的产品:
Smartphone X:$1,124,987.50 收入(1250 件,单价 $899.99)
Laptop Pro:$1,156,991.10 收入(890 件,单价 $1299.99)
Wireless Earbuds:$272,979.00 收入(2100 件,单价 $129.99)
应用 2:数据分析 - 温度监测
====================================================
前 3 个最热的城市:
Phoenix:40.2°C
Houston:35.7°C
Miami:33.8°C
前 3 个最冷的城市:
Seattle:22.4°C
Boston:27.9°C
Los Angeles:28.3°C
应用 3:社交媒体 - 用户参与度
=============================================
参与度最高的前 3 名帖子:
帖子 ID:1006
标题:Interview with Celebrity
参与度得分:11560
(点赞:4200,评论:580,分享:2200)
发布于:2023-06-15 14:25
帖子 ID:1005
标题:Recipe: Delicious Desserts
参与度得分:8450
(点赞:3100,评论:450,分享:1500)
发布于:2023-06-14 11:10
帖子 ID:1001
标题:Breaking News: Major Announcement
参与度得分:8060
(点赞:3500,评论:420,分享:1200)
发布于:2023-06-10 12:30
点赞数最高的前 3 名帖子:
Interview with Celebrity:4200 个点赞
Breaking News: Major Announcement:3500 个点赞
Recipe: Delicious Desserts:3100 个点赞
评论数最高的前 3 名帖子:
Tutorial: Python Programming:650 条评论
Interview with Celebrity:580 条评论
DIY Home Improvement Projects:520 条评论
这些示例演示了你所学到的技术如何应用于实际场景,例如电子商务销售分析、天气数据分析和社交媒体参与度指标。在每种情况下,有效地找到前 N 个元素对于从数据中提取有价值的见解至关重要。