问题求解葡萄酒
第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)