利用python实现对Excel文件中数据元组的自定义排序

问题引入:

假设你是一个浙江省水果超市的老板,统筹11个下辖地市的水果产量。假设11个地市生产的水果包括:苹果、香蕉和西瓜。你如何快速得到某种水果产量突出(排名前几)的地市?产量落后(排名后几)的地市?

问题分析:

得到某种水果产量排名前几和后几名的地市,本质是对Excel中的数据进行多次筛选,筛选的维度有:

1.水果种类;2.好排名;3.坏排名

现在假设一种情况:水果店老板想知道苹果产量排名前3的地市、香蕉产量排名前5的地市以及西瓜产量排名后4名的地市。

Excel本身可以通过多次筛选实现此功能,以苹果产量排名前3的地市为例 :

可以通过筛选选项选择苹果产量最大的3项,以降序呈现

得到结果:绍兴、嘉兴和宁波是苹果产量排名前3的地市

若避免和繁琐的Excel筛选菜单打交道,可以将此功能利用Python实现。

完整代码

python 复制代码
import openpyxl
 

file_path = "data.xlsx"
sheet_name = "Sheet2"
 
# 加载工作簿和工作表
workbook = openpyxl.load_workbook(file_path)
sheet = workbook[sheet_name]
 

fruit_id = 1
top = 3
bottom = 3
#data存储[地市-水果产量]的组合
data = []
 
# 读取数据
for row in sheet.iter_rows(min_row=2, values_only=True):  # 假设第一行是标题行,从第二行开始读取
    city = row[0]
    development = row[fruit_id] 
    data.append((city, development))
 
# 将数据按水果产量降序排序
sorted_data = sorted(data, key=lambda x: x[1], reverse=True)
 
# 获取水果产量前三名和后三名的地市
top_cities = [city for city, _ in sorted_data[:top]]
bottom_cities = [city for city, _ in sorted_data[-bottom:]]



print("【本日浙江省分地市水果产量情况】", end = '')
print(sheet.cell(row = 1,  column = fruit_id + 1).value)

print("👍️", end='')
for city in top_cities:
    print(city + " ", end='')
print("产量较高,排名前",top,"名")
print("❗", end='')
for city in bottom_cities:
    print(city + " ", end='')
print("产量较低,排名后",bottom,"名")

需求抽象

之前提到,筛选的维度包括1.水果种类;2.好排名;3.坏排名。

python 复制代码
fruit_id = 3 #西瓜
top = 3 # 前3名
bottom = 4 #后4名

fruit_id代表水果种类,1、2、3分别代表苹果、香蕉和西瓜;top代表前x的排名,若关心前3名的地市,则top = 3;bottom代表后x的排名,若关心后4名的地市,bottom = 4.

抽象出了产品维度之后,对各地市的水果产量进行排序:

python 复制代码
data = []
 # 读取数据
for row in sheet.iter_rows(min_row=2, values_only=True):  # 第一行是标题行,从第二行开始读取
    city = row[0]
    development = row[fruit_id] 
    data.append((city, development))

data数组存储着(地市-水果产量)的组合。row为for循环的迭代变量,可以理解为每个row为一个数组,row[0]为数组的第一个元素,对应于Excel中A列中的元素(0可以理解为数组里的下标,列的标号从0开始),并将row[0]的值赋给city;

同理,将fruit_id对应的水果产量row[fruit_id]的值赋给development;

data.append((city, development))将city和development封装在(city, development)元组中构成(地市-水果产量)组合,并随着for循环将11组(地市-水果产量)存储在data数组中。

python 复制代码
# 将数据按水果产量降序排序
sorted_data = sorted(data, key=lambda x: x[1], reverse=True)
 # 获取水果产量前三名和后三名的地市
top_cities = [city for city, _ in sorted_data[:top]]
bottom_cities = [city for city, _ in sorted_data[-bottom:]]

sorted_data利用sort函数,对data里的(地市-水果产量)组合进行排序,排序的主键是(地市-水果产量)中的水果产量(x[1]中的1为下标,表示元组中的第二个元素),reverse = True为降序排序。

排序后,可以在sorted_data数组中得到某水果产量前几和后几的地市的信息。由于sorted_data为降序(由大到小),则top代表前几,top_cities = [city for city, _ in sorted_data[:top]]存储产量为前top的地市;bottom_cities = [city for city, _ in sorted_data[-bottom:]]存储产量为后bottom的地市。

python 复制代码
print("【本日浙江省分地市水果产量情况】", end = '')
print(sheet.cell(row = 1,  column = fruit_id + 1).value)

print("👍️", end='')
for city in top_cities:
    print(city + " ", end='')
print("产量较高,排名前",top,"名")
print("❗", end='')
for city in bottom_cities:
    print(city + " ", end='')
print("产量较低,排名后",bottom,"名")

最后进行输出,并加以点评

输出结果

python 复制代码
fruit_id = 1
top = 3
bottom = 4 #求苹果产量的前3名和后4名

控制台输出:

python 复制代码
【本日浙江省分地市水果产量情况】苹果
👍️绍兴 嘉兴 宁波 产量较高,排名前 3 名
❗舟山 湖州 衢州 金华 产量较低,排名后 4 名
python 复制代码
fruit_id = 3
top = 5
bottom = 2 #求西瓜产量的前5名和后2名

控制台输出:

python 复制代码
【本日浙江省分地市水果产量情况】西瓜
👍️湖州 衢州 台州 丽水 宁波 产量较高,排名前 5 名
❗嘉兴 杭州 产量较低,排名后 2 名
相关推荐
暴力袋鼠哥1 小时前
基于YOLO11的车牌识别分析系统
python
满怀10154 小时前
【Python进阶】列表:全面解析与实战指南
python·算法
小小菜鸟,可笑可笑4 小时前
Python 注释进阶之Google风格
开发语言·python
upp4 小时前
[bug]langchain agent报错Invalid Format: Missing ‘Action Input:‘ after ‘Action:‘
javascript·python·langchain·bug
小技与小术4 小时前
代码随想录算法训练营day4(链表)
数据结构·python·算法·链表
前端开发张小七5 小时前
每日一练:4.有效的括号
前端·python
gongzairen5 小时前
Ngrok 内网穿透实现Django+Vue部署
后端·python·django
小小毛桃5 小时前
在PyTorch中,使用不同模型的参数进行模型预热
人工智能·pytorch·python
cliffordl5 小时前
ReportLab 导出 PDF(图文表格)
python·pdf
杰瑞学AI6 小时前
LeetCode详解之如何一步步优化到最佳解法:27. 移除元素
数据结构·python·算法·leetcode·面试·职场和发展