利用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为一个数组,row0为数组的第一个元素,对应于Excel中A列中的元素(0可以理解为数组里的下标,列的标号从0开始),并将row0的值赋给city;

同理,将fruit_id对应的水果产量rowfruit_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里的(地市-水果产量)组合进行排序,排序的主键是(地市-水果产量)中的水果产量(x1中的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 名
相关推荐
“AI国潮设计-小江”10 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞翻糖吐司”构图,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
wenwen_chao10 小时前
Python学习与开发资源合集:实用技巧与工具推荐
python
for_ever_love__11 小时前
函数——参数、lambda、*args与作用域
python·函数·大模型开发·编程基础
东莞市云毅网络有限公司11 小时前
企业资料时效性判定:Last-Modified、正文日期与版本字段三路交叉
python·sqlite·自动化运维·geo·数据监测
Query*11 小时前
深入浅出LangGraph【一】_基础篇
python·ai·langchain
XiaoMaqqqq12 小时前
市面上正规的IP驱动产业新场景新工具哪家强
运维·python·网络协议·tcp/ip
ShyanZh12 小时前
【Python3基础】19-Socket 与 TCP、UDP 编程
python·网络协议·tcp/ip·udp
惊讶的猫13 小时前
工具选对之后还不够:商城 Agent 的参数与权限治理
开发语言·python
天赐范式13 小时前
天赐范式第186天:让方差开始自洽——Bulmer效应的闭式验证
python·数字生命·天赐范式·动态运行时·遗传方差·bulmer效应
泡茶喝茶写代码13 小时前
A股量化数据工程:从 REST 接口到策略信号(第 8 篇):成长能力因子:营收与利润增速
java·python·股票数据api·股票数据api接口·股票量化数据api·股票量化数据接口·股票数据api数据