简体   繁体   English

Python 3.3 IndexError没有意义

[英]Python 3.3 IndexError doesn't make sense

I have a multidimensional array in py3.3, looks like 我在py3.3中有一个多维数组,看起来像

[ # 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. 这给了我一个IndexError,列出了超出范围的索引。 However, when I run print(month) I get each month printed out just fine. 但是,当我运行print(month)每个月都可以打印出来。

And when I set the printed month as a variable, say, x , in the shell, I can print(x[1]) just fine. 当我在外壳中将打印月份设置为变量x ,我可以正常print(x[1]) But print(month[1]) still fails. 但是print(month[1])仍然失败。 Very confused. 很迷茫。

Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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