You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

英文日期转尼泊尔日期报错:AttributeError: 'list' object has no attribute 'get'

解决英文日期转尼泊尔日期时的AttributeError问题

嘿,这个错误其实很好定位——你肯定是在处理尼泊尔年月列表的时候,不小心把**列表(list)当成字典(dict)**来操作了!get()是字典的专属方法,用来安全获取键对应的值,列表根本没有这个属性,所以才会弹出'list' object has no attribute 'get'的报错。

问题根源

你提到手里有“有效范围内每年各月份天数的列表”,如果这个数据是嵌套列表的结构(比如[[年份1, [月1天数, 月2天数...]], [年份2, [...]]]),那直接用get()肯定不行,得用索引来访问;如果是你误把列表变量当成字典来调用get(),那自然会报错。

解决方案

我给你两种处理思路,根据你的数据结构选就行:

1. 如果数据是嵌套列表结构

假设你的尼泊尔日期列表长这样:

nepali_calendar = [
    [2000, [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31]],
    [2001, [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31]],
    # 更多年份数据...
]

要获取指定年份和月份的天数,得先遍历列表找到对应年份,再通过索引取月份天数:

# 假设你已经拿到了输入的engYear、engMonth
target_year = engYear
target_month = engMonth  # 注意:如果列表里的月份是从0开始索引,这里要减1

for year_entry in nepali_calendar:
    if year_entry[0] == target_year:
        # 比如输入的是1月,对应列表索引0
        month_days = year_entry[1][target_month - 1]
        print(f"对应尼泊尔日期的该月天数:{month_days}")
        break
else:
    print("输入的年份不在尼泊尔日历的有效范围内")

2. 转成字典结构更方便(推荐)

如果把列表转成字典,用get()就完全没问题了,代码也更简洁:

# 把嵌套列表转成字典,键是年份,值是对应月份天数的列表
nepali_calendar_dict = {
    2000: [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
    2001: [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
    # 更多年份数据...
}

# 安全获取指定年份的月份天数列表,不存在则返回空列表
month_days_list = nepali_calendar_dict.get(target_year, [])
if month_days_list and 1 <= target_month <= 12:
    month_days = month_days_list[target_month - 1]
    print(f"对应尼泊尔日期的该月天数:{month_days}")
else:
    print("年份或月份不在有效范围内")

小提示

调试的时候可以先print(你的尼泊尔列表变量),确认它的结构到底是什么,这样就能快速找到哪里用错了方法~

内容的提问来源于stack exchange,提问作者Jin Sei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:46:28