Python初学者如何查找life-expectancy.csv中最接近给定值的条目
现有基础能力说明
你已经完成的功能基于life-expectancy.csv数据集实现,数据集包含四类字段:
- Entity:国家名称
- Code:国家ISO代码
- Year:统计年份
- Life expectancy (years):对应年份的人均预期寿命
你已编写的代码可实现以下输出:
- 全量数据集范围内的最高/最低预期寿命,及对应所属国家、统计年份
- 接收用户输入的目标兴趣年份,输出该年份下所有国家的:
- 预期寿命平均值
- 最高预期寿命及对应国家
- 最低预期寿命及对应国家
你的现有可运行代码如下:
import csv i = 0 j = 0 average = 0 sum_age = 0 max_age = -1 max_year = -1 max_country = "" min_age = 1000 min_year = 1000 min_country = "" lowest_age = 100 lowest_year = 1000 lowest_country = "" highest_age = -1 highest_year = -1 highest_country = "" interest = int(input("Enter the year of interest: ")) print() with open("life-expectancy.csv") as life_expct: for line in life_expct: i = i + 1 clean_line = line.strip() splitting = clean_line.split(",") if i > 1: country = splitting[0] year = int(splitting[2]) age = float(splitting[3]) if max_age < age: max_age = age max_year = year max_country = country if min_age > age: min_age = age min_year = year min_country = country if interest == year: sum_age += age j = j + 1 if highest_age < age: highest_age = age highest_year = year highest_country = country if lowest_age > age: lowest_age = age lowest_year = year lowest_country = country average = sum_age / j print(f"The overall max life expectancy is: {max_age} from {max_country} in {max_year}") print(f"The overall min life expectancy is: {min_age} from {min_country} in {min_year}") print() print(f"For the year {interest}:") print(f"The average life expectancy across all countries was {average:.2f}") print(f"The max life expectancy was in {highest_country} with {highest_age}") print(f"The min life expectancy was in {lowest_country} with {lowest_age}") print()
现有代码的运行效果示例(输入兴趣年份1965时):
Enter the year of interest: 1965 The overall max life expectancy is: 86.751 from Monaco in 2019 The overall min life expectancy is: 17.76 from Iceland in 1882 For the year 1965: The average life expectancy across all countries was 57.55 The max life expectancy was in Sweden with 73.81 The min life expectancy was in Mali with 29.489
待实现的拓展功能参考逻辑
你已掌握的列表查找最接近值的核心逻辑为:
- 计算列表内每个元素与目标值的绝对差值
- 选取差值最小的元素作为目标结果
这个逻辑完全可以直接集成到你现有代码中,不需要额外引入复杂的第三方库,适配你当前逐行读取文件的编写习惯。
可直接练习的完整修改版代码
改动点说明:
- 新增1个用户输入:需要匹配的目标预期寿命值
- 新增变量追踪流程:在逐行读取、筛选目标年份数据的同时,同步计算当前条目与目标预期寿命的差值,记录最小差值对应的国家和预期寿命值
- 新增1行输出:打印最接近目标预期寿命的条目信息
import csv # 原有基础变量初始化 i = 0 j = 0 average = 0 sum_age = 0 max_age = -1 max_year = -1 max_country = "" min_age = 1000 min_year = 1000 min_country = "" lowest_age = 100 lowest_year = 1000 lowest_country = "" highest_age = -1 highest_year = -1 highest_country = "" # 新增用于匹配最接近值的变量初始化 min_diff = 1000 # 初始设为远大于可能差值的数值,方便后续更新 closest_country = "" closest_exp = 0 # 原有输入逻辑 + 新增目标值输入 interest = int(input("Enter the year of interest: ")) target_exp = float(input("Enter the target life expectancy to match: ")) print() with open("life-expectancy.csv") as life_expct: for line in life_expct: i = i + 1 clean_line = line.strip() splitting = clean_line.split(",") if i > 1: country = splitting[0] year = int(splitting[2]) age = float(splitting[3]) # 原有:全量数据集的最大/最小预期寿命计算 if max_age < age: max_age = age max_year = year max_country = country if min_age > age: min_age = age min_year = year min_country = country # 原有:目标年份的统计逻辑 if interest == year: sum_age += age j = j + 1 if highest_age < age: highest_age = age highest_year = year highest_country = country if lowest_age > age: lowest_age = age lowest_year = year lowest_country = country # 新增:计算当前条目与目标预期寿命的差值,更新最接近结果 current_diff = abs(age - target_exp) if current_diff < min_diff: min_diff = current_diff closest_country = country closest_exp = age average = sum_age / j # 原有输出逻辑 print(f"The overall max life expectancy is: {max_age} from {max_country} in {max_year}") print(f"The overall min life expectancy is: {min_age} from {min_country} in {min_year}") print() print(f"For the year {interest}:") print(f"The average life expectancy across all countries was {average:.2f}") print(f"The max life expectancy was in {highest_country} with {highest_age}") print(f"The min life expectancy was in {lowest_country} with {lowest_age}") # 新增输出最接近目标值的条目 print(f"The life expectancy closest to {target_exp} was in {closest_country} with {closest_exp:.3f}") print()
运行效果示例(输入年份1965、目标预期寿命60时):
Enter the year of interest: 1965 Enter the target life expectancy to match: 60 The overall max life expectancy is: 86.751 from Monaco in 2019 The overall min life expectancy is: 17.76 from Iceland in 1882 For the year 1965: The average life expectancy across all countries was 57.55 The max life expectancy was in Sweden with 73.81 The min life expectancy was in Mali with 29.489 The life expectancy closest to 60.0 was in Chile with 59.921
内容的提问来源于stack exchange,提问作者Gustavo Azevedo
相关产品推荐
相关产品推荐

