简体   繁体   中英

Python 3.3 IndexError doesn't make sense

I have a multidimensional array in py3.3, looks like

[ # big container

  [ # month container

    ['date string', 'temp'],['date string', 'temp'] #dates in month

  ], #close month container

  [ # next month container

    ['date string', 'temp'], ['date string', 'temp']

  ]

]

Here is my code:

dailyDict = []
dailyRow = []
compareStation = 'myfile.csv'
with open(compareStation, newline='\n') as csvfile:
            station = csv.reader(csvfile, delimiter=',', quotechar='|')
            for row in station:
                if 1stDayOfMonthCondition:
                    dailyDict.append(dailyRow)
                    dailyRow = []
                    dailyRow.append(row)
                else:
                    dailyRow.append(row)

for month in dailyDict:
        print(month[1])

this gives me an IndexError, list index out of range. However, when I run print(month) I get each month printed out just fine.

And when I set the printed month as a variable, say, x , in the shell, I can print(x[1]) just fine. But print(month[1]) still fails. Very confused.

Thanks.

索引列表从0而不是1开始,因此您应该尝试使用print(month[0])来查看是否有这种方式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM