求元组列表的平均值

2024-06-25 22:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮帮我!我在这方面是个新手,我想我已经完全疯了。在

我得到了一个csv文件来使用和推断数据。有一个相当广泛的国家名单,有相应的国内生产总值,失业率等等。我很吝啬。提供国家最高的国内生产总值,2。确定这个国家的失业率是否低于平均水平。我可以很好地理解数据定义(我想),但是当我试图在给定的列表中找到平均失业率时,我遇到了一些问题(我使用命名tuple按失业率和国家名称组织列表)。在

def highest_gdp(logdp:List[CountryGDP])-> str: 
    """
    returns the name of the country with the highest GDP
    """
    #Return "" #body of the stub
    #template from List[CountryGDP] 

    highest = logdp[0] #type: CountryGDP
    for gdp in logdp:
        if gdp.cgdp >= highest.cgdp:
            highest = gdp
        return highest.name


def average_unemployment(louemp:List[Unemployment])-> float:
    """
    returns the average unemployment rate of the given list
    """
    #return 0 #body of the stub
    #template from List[Unemployment]

    average = louemp[0] #type: Unemployment
    for uemp in louemp:
        if uemp.rate > 0: 
            average = uemp
        return sum(uemp.rate)/float(len(louemp))

我认为第一部分是正确的。当我运行它时没有返回错误,但我似乎无法计算出第二个将返回平均失业率的错误!!我总是收到各种各样的错误信息,不管我怎么尝试!我知道这是超级基础,但请帮助!!!在


Tags: ofthereturnrate国家listaveragegdp
1条回答
网友
1楼 · 发布于 2024-06-25 22:51:14

我认为您需要将return语句放在for循环之外。对于这两种情况。但我没有检查逻辑。在

def highest_gdp(logdp:List[CountryGDP])-> str: 
    """
    returns the name of the country with the highest GDP
    """
    #Return "" #body of the stub
    #template from List[CountryGDP] 

    highest = logdp[0] #type: CountryGDP
    for gdp in logdp:
        if gdp.cgdp >= highest.cgdp:
            highest = gdp
    return highest.name


def average_unemployment(louemp:List[Unemployment])-> float:
    """
    returns the average unemployment rate of the given list
    """
    #return 0 #body of the stub
    #template from List[Unemployment]

    average = louemp[0] #type: Unemployment
    for uemp in louemp:
        if uemp.rate > 0: 
            average = uemp
    return sum(uemp.rate)/float(len(louemp))

相关问题 更多 >