【问题标题】:How do I find the Max or Min value from this dataset in Python?如何在 Python 中从此数据集中找到最大值或最小值?
【发布时间】:2021-06-15 09:29:51
【问题描述】:

我正在使用全球预期寿命的在线数据集,并试图在 life_expectancy 列中找到最大值和最小值。

这里是数据集:https://ourworldindata.org/spanish-flu-largest-influenza-pandemic-in-history

这是我在尝试其他帖子中建议的数学方程式和 max() 和 min() 后得到的结果。

with open('data/life-expectancy.csv') as life_expectancy:
    next(life_expectancy)
    for data in life_expectancy:
        clean_data = data.strip()
        split_data = clean_data.split(',')

        entity = split_data[0]
        code = split_data[1]
        year = split_data[2]
        expectancy = float(split_data[3])
              
print(f'The overall max life expectancy is: {max(split_data[3])}')
print(f'The overall min life expectancy is: {min(split_data[3])}')

我还应该添加什么才能真正获得正确的结果?

电流输出:

The overall max life expectancy is: 9
The overall min life expectancy is: .

【问题讨论】:

  • 你的数据是什么样的?
  • 数据集的链接不起作用..
  • for 循环完成后,您希望程序中每个变量的值是多少?当您致电max 时,您认为应该传递给它什么?根据您的理解,这是否与您的代码传递给它的内容相匹配?现在,测试你的假设。另请阅读ericlippert.com/2014/03/05/how-to-debug-small-programs

标签: python max min


【解决方案1】:

您没有对正在迭代的数据做任何事情。

当您将数据存储在列表中时,我们可以在数据集上使用minmax。使用键和lambda,我们可以确保我们的结果包含所有相关数据,而不仅仅是存储最大值。

with open('life-expectancy.csv') as life_expectancy:
    next(life_expectancy)
    
    ## Create an empty list
    output = []
    
    for data in life_expectancy:
        clean_data = data.strip()
        split_data = clean_data.split(',')

        entity = split_data[0]
        code = split_data[1]
        year = split_data[2]
        expectancy = float(split_data[3])
      
        ## Append to the list
        output.append([entity, code, year, expectancy])

max_life = max(output, key=lambda x: x[3])
min_life = min(output, key=lambda x: x[3])

#['Monaco', 'MCO', '2019', 86.751]
#['Iceland', 'ISL', '1882', 17.76]

print(f'The overall max life expectancy is {max_life[3]} in {max_life[0]}')    
print(f'The overall min life expectancy is {min_life[3]} in {min_life[0]}')

#The overall max life expectancy is 86.751 in Monaco
#The overall min life expectancy is 17.76 in Iceland

为了提高可读性,您可以通过修改以下行将数据存储为 `dicts 列表

output.append({'entity': entity, 'code': code, 'year': year, 'expectancy': expectancy})

max_life = max(output, key=lambda x: x['expectancy'])
min_life = min(output, key=lambda x: x['expectancy'])

print(f'The overall max life expectancy is {max_life["expectancy"]} in {max_life["entity"]}')
print(f'The overall min life expectancy is {min_life["expectancy"]} in {min_life["entity"]}')

【讨论】:

  • 感谢您的帮助。我确实有一些关于为什么“key=lambda x: x[3]”是这样布局的问题?我还是新手,所以这超出了我的理解哈哈。
  • realpython.com/python-lambda 会有所帮助。在这种情况下,它允许我们在元素 3 中找到具有 maxmin 值的列表
  • 只是出于好奇,还有其他东西可以代替 lambda 的角色吗?我只是想从视觉上看它在输出旁边做什么,因为没有它我会在尝试组合字符串和列表时遇到错误。
【解决方案2】:

您想创建在循环时建立的列表,然后再取最小值/最大值。

with open('data/life-expectancy.csv') as life_expectancy:
    next(life_expectancy)

    entities = []
    codes = []
    years = []
    expectancies = []
    for data in life_expectancy:
        clean_data = data.strip()
        split_data = clean_data.split(',')

        entities.append(split_data[0])
        codes.append(split_data[1])
        years.append(split_data[2])
        expectancies.append(float(split_data[3]))
              
print(f'The overall max life expectancy is: {max(expectancies)}')
print(f'The overall min life expectancy is: {min(expectancies)}')

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 2011-06-28
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2011-12-02
    • 2015-06-29
    • 2023-01-27
    相关资源
    最近更新 更多