6.1 葡萄酒评论分析报告

任务描述

本关任务:编写程序,多维度分析葡萄酒数据。


第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:葡萄酒数据,列表类型
    """
    # 此处补充你的代码
    country_list = []
    for x in wine_list:
        if x[COUNTRY] not in country_list:
            country_list.append(x[COUNTRY])
    country_list.sort()
    return country_list
 

def avg_point(wine_list, country):
    """接收列表格式的葡萄酒数据和国家名列表为参数,计算每个国家的葡萄酒的平均得分,
    返回值为国家名和得分的列表。
    @参数 wine_list:葡萄酒数据,列表类型
    @参数 country:国家名,列表类型
    """
    # 此处补充你的代码
    score=[]
    for i in range(len(country)):
        n=0
        count=0
        for j in wine_list:
            if country[i]==j[1]:
                n+=float(j[-3])
                count+=1
        score.append([country[i],round(n/count,2)])
    ls=sorted(score,key=lambda x:x[0], reverse=False)
    return ls

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:国家名,列表类型
    """
    # 此处补充你的代码
    score=[]
    for i in range(len(country)):
        n=0
        count=0
        for j in wine_list:
            if country[i]==j[1]:
                n+=float(j[-3])
                count+=1
        score.append([country[i],round(n/count,2)])
    ls=sorted(score,key=lambda x:x[1], reverse=True)
    return ls


def top_10_point(wine_list):
    """接收列表格式的葡萄酒数据参数,返回评分最高的十款葡萄酒的编号、出产国、评分和价格,按评
    分降序输出。
    需要注意的是评分可能有缺失值,此时该数据为nan
    if math.isnan(x) == False可用于判定x的值是不是nan
    nan的数据类型是float,不可以直接用字符串判定方法。
    @参数 wine_list:葡萄酒数据,列表类型
    """
    # 此处补充你的代码
    list1=[]
    for i in wine_list:
        if math.isnan(float(i[-3]))==False:
            list1.append(i)
    list2=sorted(list1,key=lambda x:x[-3],reverse=True)
    list3=[]
    count=1
    for k in list2:
        if count<11:
            list3.append([k[-6],k[-5],k[-3],k[-2]])
            count+=1
    return list3


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):
    """接收文件名为参数,用pandas读取文件中的数据,数据部分转为二维列表类型,返回二维列表。"""
    wine_list = pd.read_csv(file).values.tolist()
    return wine_list


def top_20_price(wine_list):
    """接收列表格式的葡萄酒数据参数,返回价格最高的二十款葡萄酒的编号、出产国、评分和价格,按价
    格降序输出。
    @参数 wine_list:葡萄酒数据,列表类型
    需要注意的是价格可能有缺失值,此时该数据为nan
    if math.isnan(x) == False可用于判定x的值是不是nan
    nan的数据类型是float,不可以直接用字符串判定方法。
    """
    # 此处补充你的代码
    list1=[]
    for i in wine_list:
        if math.isnan(float(i[-2]))==False:
            list1.append(i)
    list2=sorted(list1,key=lambda x:x[-2],reverse=True)
    list3=[]
    count=1
    for k in list2:
        if count<21:
            list3.append([k[-6],k[-5],k[-3],k[-2]])
            count+=1
    return list3

def amount_of_point(wine_list):
    """接收列表格式的葡萄酒数据参数,返回每个评分的葡萄酒数量,忽略没有评分的数据
    例如[...[84, 645], [85, 959],...]表示得分为84的葡萄酒645种,得分85的葡萄酒有959种。
    @参数 wine_list:葡萄酒数据,列表类型
    """
    # 此处补充你的代码
    score_list = []
    list1 = []
    count = 0
    for x in wine_list:
        if x[-3] not in score_list:
            score_list.append(x[-3])
    score_list.sort()
    for i in score_list:
        count = 0
        for j in wine_list:
            if i == int(j[-3]):
                count += 1
        list1.append([i, count])
    return list1


def most_of_point(amount_of_points):
    """接收每个评分的葡萄酒数量的列表为参数,返回获得该分数数量最多的评分和数量的列表。
    @参数 amount_of_points:每个评分的葡萄酒数量,列表类型
    """
    # 此处补充你的代码
    list1 = sorted(amount_of_points, key=lambda x: x[1], reverse=True)
    return list1[0]


def avg_price_of_most_point(wine_list, most_of_points):
    """接收列表格式的葡萄酒数据和获得最多的评分及数量的列表为参数
    忽略缺失价格的数据,返回这个分数的葡萄酒的平均价格,保留2位小数。
    @参数 wine_list:葡萄酒数据,列表类型
    @参数 most_of_points:获得最多的评分及数量,列表类型
    """
    # 此处补充你的代码
    sum = 0
    m = 0
    for i in wine_list:
        if most_of_points[0] == int(i[-3]):
            if math.isnan(float(i[-2])) == False:
                sum += float(i[-2])
                m += 1
    return round(sum / m, 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)
        most_point = most_of_point(amount_point)
        print(amount_point)  # 各个评分的葡萄酒数量
        print(most_point)  # 拥有葡萄酒数量最多的评分和数量
        print(avg_price_of_most_point(wine, most_point))  # 拥有葡萄酒数量最多的评分的葡萄酒的平均价格
    else:
        print('输入错误')


if __name__ == '__main__':
    text = input()
    judge(text)

尝试静下心来做自己该做的的事情,不要让焦虑和烦恼毁掉你本就不多的热情与定力。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年11月6日
下一篇 2023年11月6日

相关推荐