我不断收到错误消息IndexError:列表分配索引超出范围
schedule = []
for i in range(18):
schedule.append( set() )
schedule[1] = { ('LAR','CP'), ('KCC','JJ'), ('BR','MD') }
这为什么不起作用?
编辑:用于排定[1],而非排定[18]
答案 0 :(得分:-3)
将最终分配移出循环。
schedule = []
for i in range(18):
schedule.append( set() )
schedule[1] = { ('LAR','CP'), ('KCC','JJ'), ('BR','MD') }
做我想做的更简单的方法:
# Make the list of empty sets:
schedule = [set()] * 18
# or
schedule = [set() for _ in range(18)]
# Then you assign the value in the list at index 1 to the set you want:
schedule[1] = { ('LAR','CP'), ('KCC','JJ'), ('BR','MD') }
编辑:问题已更改并解决了评论。