【问题标题】:"AttributeError: 'list' object has no attribute 'get' ""AttributeError: 'list' 对象没有属性 'get'"
【发布时间】:2021-10-02 02:28:23
【问题描述】:

我正在尝试将英文日期转换为等效的尼泊尔日期。

当我运行我得到的代码时

AttributeError: 'list' 对象没有属性 'get'

这是我的代码:

from datetime import date
import datetime 
import time
import calendar

engMonth = int(input('Enter birth month in A.D.'))
engDate = int(input('Enter birth date in A.D.'))
engYear = int(input('Enter birth year in A.D.'))

以下是编号列表。每年每个月在有效范围内的天数

尼泊尔年月列表,其中 2000、2001、2002 等为尼泊尔年,30、32、31 等为月份总天数

nepaliMonths = [
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],  #2000
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],  #2001
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],
            [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],  #2071
            [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],  #2072
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31 ],  #2073
            [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
            [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30 ],
            [ 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 ],
            [ 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],  #2090
            [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30 ],
            [ 30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30 ],
            [ 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30 ],
            [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30 ],
            [ 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31 ],
            [ 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30 ]   #2099
        ]


#define the least possible English date 1944/01/01 Saturday.

startingEngYear = 1944
startingEngMonth = 1
startingEngDay = 1
dayOfWeek = calendar.SATURDAY  #1944 is a saturday


#Let's define the equivalent Nepali date 2000/09/17.

startingNepYear = 2000
startingNepMonth = 9
startingNepday = 17


# Let's calculate the number of days between the two English dates as follows:

date0=date(engYear,engMonth,engDate)
date1=date(startingEngYear,startingEngMonth,startingEngDay)
delta=date0 - date1
# print (delta.days)


#initialize required nepali date variables with starting  nepali date
nepYear = startingNepYear
nepMonth = startingNepMonth
nepDay = startingNepday

此代码块中有错误。

# decrement delta.days until its value becomes zero
while delta.days != 0:

    # getting total number of days in month nepMonth in a year nepYear
    daysInMonth = nepaliMonths.get(nepYear)[nepMonth]
    nepDay+=1 # incrementing nepali day

    if(nepDay > daysInMonth):
        nepMonth+=1
        nepDay = 1

    if(nepMonth > 12):
        nepYear+=1
        nepMonth = 1

    dayOfWeek+=1 #counting the days in terms of 7 days
    if(dayOfWeek > 7):
        dayOfWeek = 1

    delta.days-=1   

帮助

【问题讨论】:

  • 该错误非常具有描述性。你不能用get()访问list
  • 您使用 nepyear 作为 2000 的 idex ...即使您删除 .get 您仍然会遇到 idex out of range 。问题在于您的 nepyear 列表

标签: python python-3.x


【解决方案1】:

为什么不使用Python dictionary?

有了字典,你可以有这样的东西:

nepaliMonths = { 2000: [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31 ],
                 2001: [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30 ],

                 # Fill with rest of data . . .

                 2099: [ 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30 ] }

然后,要索引您的数据集,只需使用 nepaliMonths[year][month - 1]

示例:nepaliMonths[2000][0] == 30 和 nepaliMonths[2000][11] == 31

另一种方法:

使用您当前的实现,您只需使用nepaliMonths[year - 2000][month - 1]

示例:nepaliMonths[0][0] == 30 和 nepaliMonths[0][11] == 31

【讨论】:

    【解决方案2】:

    nepaliMonths 是一个列表,实际上,列表没有.get() 方法。

    也许你的意思是nepaliMonths[nepYear - startingNepYear][nepMonth]?

    【讨论】:

    • @JohnGordon 我试过你的方法,得到“IndexError: list index out of range”错误
    猜你喜欢
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2023-04-09
    • 2016-01-30
    • 2020-12-15
    • 2016-04-15
    相关资源
    最近更新 更多