当前位置: 首页 > news >正文

问题求解葡萄酒

第1关葡萄酒评论分析报告——国家列表和平均分import pandas as pd # 定义符号常量用于索引使之具有清晰的语义 COUNTRY 1 POINTS 3 def csv_to_ls(file): 接收文件名为参数用pandas读取文件中的数据数据部分转为二维列表类型返回二维列表。 wine_list pd.read_csv(file).values.tolist() return wine_list def country_ls(wine_list): 接收列表格式的葡萄酒数据为参数略过标题行返回不重复的国家名列表按字母表升序排序 若国家名数据缺失略过该条数据返回值中不包含空字符串元素。 参数 wine_list葡萄酒数据列表类型 countries set() for row in wine_list[1:]: country row[COUNTRY] if pd.notna(country) and country.strip() ! : countries.add(country) return sorted(countries) def avg_point(wine_list, country): 接收列表格式的葡萄酒数据和国家名列表为参数计算每个国家的葡萄酒的平均得分 返回值为国家名和得分的列表。 参数 wine_list葡萄酒数据列表类型 参数 country国家名列表类型 result[] for c in country: total0 count0 for row in wine_list[1:]: if row[COUNTRY]c: total row[POINTS] count 1 avg round(total/count,2) result.append([c,float(avg)]) return result def judge(txt): 接收一个字符串为参数根据参数值调用不同函数完成任务 filename data/winemag-data.csv wine csv_to_ls(filename) country country_ls(wine) if txt 国家名列表: print(country) elif txt 平均分: print(avg_point(wine, country)) # 每个国家的葡萄酒的平均得分 else: print(输入错误) if __name__ __main__: text input() judge(text)第2关葡萄酒评论分析报告——平均分排序和评分最高import pandas as pd import math # 定义符号常量用于索引使之具有清晰的语义 NUMBER 0 COUNTRY 1 DESCRIPTION 2 POINTS 3 PRICE 4 def csv_to_ls(file): 接收文件名为参数用pandas读取文件中的数据数据部分转为二维列表类型返回二维列表。 wine_list pd.read_csv(file).values.tolist() return wine_list def country_ls(wine_list): 接收列表格式的葡萄酒数据为参数略过标题行返回不重复的国家名列表按字母表升序排序 若国家名数据缺失略过该条数据返回值中不包含空字符串元素。 参数 wine_list葡萄酒数据列表类型 country_list [] for x in wine_list: if x[COUNTRY] not in country_list: country_list.append(x[COUNTRY]) country_list.sort() # print(country_list) return country_list def avg_point_sort(wine_list, country): 接收列表格式的葡萄酒数据和国家名列表为参数计算每个国家的葡萄酒的平均得分 返回值为国家名和得分的列表按评分由高到低降序排列。 参数 wine_list葡萄酒数据列表类型 参数 country国家名列表类型 result [] for c in country: total 0 count 0 for row in wine_list[1:]: if row[COUNTRY] c: total row[POINTS] count 1 if count 0: avg 0.0 else: avg round(total / count, 2) result.append([c, avg]) result.sort(keylambda x: x[1], reverseTrue) return result def top_10_point(wine_list): 接收列表格式的葡萄酒数据参数返回评分最高的十款葡萄酒的编号、出产国、评分和价格按评 分降序输出。 需要注意的是评分可能有缺失值此时该数据为nan if math.isnan(x) False可用于判定x的值是不是nan nan的数据类型是float,不可以直接用字符串判定方法。 参数 wine_list葡萄酒数据列表类型 valid_wines [] for row in wine_list[1:]: if math.isnan(row[POINTS]): continue number row[NUMBER] country row[COUNTRY] points row[POINTS] price row[PRICE] valid_wines.append([number, country, points, price]) valid_wines.sort(keylambda x: x[2], reverseTrue) return valid_wines[:10] def judge(txt): 接收一个字符串为参数根据参数值调用不同函数完成任务 filename data/winemag-data.csv wine csv_to_ls(filename) country country_ls(wine) if txt 平均分排序: print(avg_point_sort(wine, country)) # 每个国家的葡萄酒的平均得分降序输出 elif txt 评分最高: print(top_10_point(wine)) # 评分最高的十款葡萄酒的编号、出产国、评分和价格按评分降序输出 else: print(输入错误) if __name__ __main__: text input() judge(text)第3关葡萄酒评论分析报告——价格最高和葡萄酒评分import pandas as pd import math NUMBER 0 COUNTRY 1 POINTS 3 PRICE 4 def csv_to_ls(file): return pd.read_csv(file).values.tolist() def top_20_price(wine_list): valid [r for r in wine_list if not math.isnan(r[PRICE])] valid.sort(keylambda x: x[PRICE], reverseTrue) return [[int(r[NUMBER]), r[COUNTRY], int(r[POINTS]), r[PRICE]] for r in valid[:20]] def amount_of_point(wine_list): cnt {} for r in wine_list: p r[POINTS] if not math.isnan(p): p int(p) cnt[p] cnt.get(p, 0) 1 return [[k, cnt[k]] for k in sorted(cnt.keys())] def most_of_point(amount_of_points): return max(amount_of_points, keylambda x: x[1]) def avg_price_of_most_point(wine_list, most_of_points): score most_of_points[0] valid [ r for r in wine_list if (not math.isnan(r[POINTS]) and int(r[POINTS]) score and not math.isnan(r[PRICE])) ] return round(sum(r[PRICE] for r in valid) / len(valid), 2) def judge(txt): filename data/winemag-data.csv wine csv_to_ls(filename) if txt 价格最高: print(top_20_price(wine)) elif txt 葡萄酒评分: amount_point amount_of_point(wine) print(amount_point) most_point most_of_point(amount_point) print(most_point) print(avg_price_of_most_point(wine, most_point)) else: print(输入错误) if __name__ __main__: text input() judge(text)
http://www.rkmt.cn/news/1295015.html

相关文章:

  • 别再只画拓扑了!用eNSP深度仿真医院网络:业务隔离、高可用与安全接入实战解析
  • Cadence IC617实战:用gm/id法搞定五管OTA运放设计,附完整参数计算与仿真避坑
  • 为什么87%的中大型企业Claude接入项目延期超6周?揭秘缺失的3层治理架构(附架构图)
  • 当机器人遇见城市:江南北如何重塑武汉的智能生活图景
  • OpenCV实战:用傅里叶变换和谱残差算法,一键找出图片里的‘视觉焦点’
  • 从布尔代数到芯片实现:深入解析加法器设计与Verilog实战
  • AI前端工程化实战指南:10大核心场景的“解题思路“与“避坑指南“
  • 对比官方价格Taotoken活动价在模型调用上的成本优势
  • 告别显示器!树莓派5无屏启动与远程配置全攻略(最新Raspberry Pi OS,含网络配置与VNC/SSH一键脚本)
  • LibreOffice Online 终极指南:如何在浏览器中实现免费办公协作
  • 如何快速掌握Winhance中文版:Windows优化终极指南
  • Heightmapper完全指南:5步将全球地形数据变成3D模型
  • RK3576 音视频网络传输总结(RTP / RTSP / UDP / H265)
  • Hermes Agent工具连接Taotoken大模型服务的配置指南
  • 终极解决方案:让苹果触控板在Windows上获得原生级精准触控体验
  • 电商冷启动实战:0.01元引流、50单破局、0差评与8.8%转化率
  • 从零到一:在面包板上构建一个4位加法器的完整实践
  • 大语言模型记忆增强框架:LightMem轻量化设计与工程实践
  • Excalidraw终极指南:快速掌握免费开源虚拟白板的完整使用技巧
  • TickGPTick:基于AI的智能任务管理助手设计与实战部署
  • CSerialPort不止于C++:手把手教你用Python/Node.js调用串口,快速构建上位机应用
  • 粤语语音合成精准度告急?ElevenLabs最新v2.5模型适配香港/广州/澳门三方口音对照表,速领!
  • 【ElevenLabs语音伦理合规白皮书】:面向银发群体的AI语音生成必须绕开的4类GDPR/《互联网信息服务深度合成管理规定》雷区
  • RK3568平台开发系列讲解(热拔插篇)内核是如何发送事件到用户空间
  • 告别反射性能损耗:Spring Boot项目实战,用MapStruct优雅替换BeanUtils
  • 拒绝死记硬背:Docker 常用命令与参数英文全称对照指南
  • Bifrost:三星固件下载与管理的终极解决方案
  • 若依微服务架构下Seata 1.5.2与Nacos的分布式事务实战配置与避坑指南
  • 终极Windows风扇控制指南:Fan Control完全教程与静音散热方案
  • Cursor Free VIP:一键解决Cursor AI试用限制的智能工具