python练习题:葡萄酒后续

2

import pandas as pd
import math

# 定义符号常量,用于索引,使之具有清晰的语义
NUMBER = 0
COUNTRY = 1
DESCRIPTION = 2
POINTS = 3
PRICE = 4


def csv_to_ls(file):
    """接收文件名为参数,逐行读取文件中的数据,根据逗号切分每行数据为列表类型,
    作为二维列表的一个元素,返回二维列表。
    @参数 file:文件名,字符串类型
    """
    wine_list = pd.read_csv(file).values.tolist()
    # print(wine_list)
    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_sort(wine_list, country):
    """接收列表格式的葡萄酒数据和国家名列表为参数,计算每个国家的葡萄酒的平均得分,
    返回值为国家名和得分的列表,按评分由高到低降序排列。
    @参数 wine_list:葡萄酒数据,列表类型
    @参数 country:国家名,列表类型
    """
    #####################Begin#####################################
    # 此处去掉注释符号“#”并补充你的代码
    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
    

    #####################End#####################################



def top_10_point(wine_list):
    """接收列表格式的葡萄酒数据参数,返回评分最高的十款葡萄酒的编号、出产国、评分和价格,按评
    分降序输出。
    需要注意的是评分可能有缺失值,此时该数据为nan
    if math.isnan(x) == False可用于判定x的值是不是nan
    nan的数据类型是float,不可以直接用字符串判定方法。
    @参数 wine_list:葡萄酒数据,列表类型
    """
    #####################Begin#####################################
    # 此处去掉注释符号“#”并补充你的代码
    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

    #####################End#####################################


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):
    """接收文件名为参数,逐行读取文件中的数据,根据逗号切分每行数据为列表类型,
    作为二维列表的一个元素,返回二维列表。
    @参数 file:文件名,字符串类型
    """
    wine_list = pd.read_csv(file).values.tolist()
    # print(wine_list)
    return wine_list


def top_20_price(wine_list):
    """接收列表格式的葡萄酒数据参数,返回价格最高的二十款葡萄酒的编号、出产国、评分和价格,按价
    格降序输出。
    @参数 wine_list:葡萄酒数据,列表类型
    需要注意的是价格可能有缺失值,此时该数据为nan
    if math.isnan(x) == False可用于判定x的值是不是nan
    nan的数据类型是float,不可以直接用字符串判定方法。
    """
    #####################Begin#####################################
    # 此处去掉注释符号“#”并补充你的代码
    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
    #####################End#####################################


def amount_of_point(wine_list):
    """接收列表格式的葡萄酒数据参数,返回每个评分的葡萄酒数量,忽略没有评分的数据
    例如[...[84, 645], [85, 959],...]表示得分为84的葡萄酒645种,得分85的葡萄酒有959种。
    @参数 wine_list:葡萄酒数据,列表类型
    """
    #####################Begin#####################################
    # 此处去掉注释符号“#”并补充你的代码
    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
    #####################End#####################################


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


def avg_price_of_most_point(wine_list, most_of_points):
    """接收列表格式的葡萄酒数据和获得最多的评分及数量的列表为参数
    忽略缺失价格的数据,返回这个分数的葡萄酒的平均价格,保留2位小数。
    @参数 wine_list:葡萄酒数据,列表类型
    @参数 most_of_points:获得最多的评分及数量,列表类型
    """
    #####################Begin#####################################
    # 此处去掉注释符号“#”并补充你的代码
    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)
    #####################End#####################################


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)

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值