为什么列表索引超出范围? [英] Why is the list index out of range?

查看:125
本文介绍了为什么列表索引超出范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是程序设计的新手,我正在尝试检查一段不断给我这个错误的代码:

I'm new at programing and I'm trying to check a piece of code that keeps giving me this error:

t[i] = t[i - 1] + dt
IndexError: list index out of range

代码如下:

dt = 0.001

t = [0]
for i in range(1, 499):
    t[i] = t[i - 1] + dt

我想将t的第一个值固定为0,然后在一个范围内定义其行为(顺便说一句,我也不知道如何固定值),但我一直遇到相同的错误.请帮忙.谢谢

I want to fix the first value of t as 0, and then define its behavior in a range (btw I'm not sure about how to fix a value neither) but I keep getting the same error. Please help. Thanks

推荐答案

我认为您正在尝试将append值添加到列表中.如果是,那么这就是您的操作方式:

I think that you are trying to append values to the list. If yes, then this is how you do it:

dt = 0.001

t = [0]
for i in range(1, 499):
    t.append(t[i - 1] + dt)

输出:

>>> t
[0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009000000000000001, 0.010000000000000002, 0.011000000000000003, 0.012000000000000004, 0.013000000000000005, 0.014000000000000005, 0.015000000000000006, 0.016000000000000007, 0.017000000000000008, 0.01800000000000001, 0.01900000000000001, 0.02000000000000001, 0.02100000000000001, 0.022000000000000013, 0.023000000000000013, 0.024000000000000014, 0.025000000000000015, 0.026000000000000016, 0.027000000000000017, 0.028000000000000018, 0.02900000000000002, 0.03000000000000002, 0.03100000000000002, 0.03200000000000002, 0.03300000000000002, 0.03400000000000002, 0.035000000000000024, 0.036000000000000025, 0.037000000000000026, 0.03800000000000003, 0.03900000000000003, 0.04000000000000003, 0.04100000000000003, 0.04200000000000003, 0.04300000000000003, 0.04400000000000003, 0.04500000000000003, 0.046000000000000034, 0.047000000000000035, 0.048000000000000036, 0.04900000000000004, 0.05000000000000004, 0.05100000000000004, 0.05200000000000004, 0.05300000000000004, 0.05400000000000004, 0.05500000000000004, 0.05600000000000004, 0.057000000000000044, 0.058000000000000045, 0.059000000000000045, 0.060000000000000046, 0.06100000000000005, 0.06200000000000005, 0.06300000000000004, 0.06400000000000004, 0.06500000000000004, 0.06600000000000004, 0.06700000000000005, 0.06800000000000005, 0.06900000000000005, 0.07000000000000005, 0.07100000000000005, 0.07200000000000005, 0.07300000000000005, 0.07400000000000005, 0.07500000000000005, 0.07600000000000005, 0.07700000000000005, 0.07800000000000006, 0.07900000000000006, 0.08000000000000006, 0.08100000000000006, 0.08200000000000006, 0.08300000000000006, 0.08400000000000006, 0.08500000000000006, 0.08600000000000006, 0.08700000000000006, 0.08800000000000006, 0.08900000000000007, 0.09000000000000007, 0.09100000000000007, 0.09200000000000007, 0.09300000000000007, 0.09400000000000007, 0.09500000000000007, 0.09600000000000007, 0.09700000000000007, 0.09800000000000007, 0.09900000000000007, 0.10000000000000007, 0.10100000000000008, 0.10200000000000008, 0.10300000000000008, 0.10400000000000008, 0.10500000000000008, 0.10600000000000008, 0.10700000000000008, 0.10800000000000008, 0.10900000000000008, 0.11000000000000008, 0.11100000000000008, 0.11200000000000009, 0.11300000000000009, 0.11400000000000009, 0.11500000000000009, 0.11600000000000009, 0.11700000000000009, 0.11800000000000009, 0.11900000000000009, 0.12000000000000009, 0.1210000000000001, 0.1220000000000001, 0.1230000000000001, 0.1240000000000001, 0.12500000000000008, 0.12600000000000008, 0.12700000000000009, 0.12800000000000009, 0.1290000000000001, 0.1300000000000001, 0.1310000000000001, 0.1320000000000001, 0.1330000000000001, 0.1340000000000001, 0.1350000000000001, 0.1360000000000001, 0.1370000000000001, 0.1380000000000001, 0.1390000000000001, 0.1400000000000001, 0.1410000000000001, 0.1420000000000001, 0.1430000000000001, 0.1440000000000001, 0.1450000000000001, 0.1460000000000001, 0.1470000000000001, 0.1480000000000001, 0.1490000000000001, 0.1500000000000001, 0.1510000000000001, 0.1520000000000001, 0.1530000000000001, 0.1540000000000001, 0.1550000000000001, 0.1560000000000001, 0.1570000000000001, 0.1580000000000001, 0.1590000000000001, 0.16000000000000011, 0.16100000000000012, 0.16200000000000012, 0.16300000000000012, 0.16400000000000012, 0.16500000000000012, 0.16600000000000012, 0.16700000000000012, 0.16800000000000012, 0.16900000000000012, 0.17000000000000012, 0.17100000000000012, 0.17200000000000013, 0.17300000000000013, 0.17400000000000013, 0.17500000000000013, 0.17600000000000013, 0.17700000000000013, 0.17800000000000013, 0.17900000000000013, 0.18000000000000013, 0.18100000000000013, 0.18200000000000013, 0.18300000000000013, 0.18400000000000014, 0.18500000000000014, 0.18600000000000014, 0.18700000000000014, 0.18800000000000014, 0.18900000000000014, 0.19000000000000014, 0.19100000000000014, 0.19200000000000014, 0.19300000000000014, 0.19400000000000014, 0.19500000000000015, 0.19600000000000015, 0.19700000000000015, 0.19800000000000015, 0.19900000000000015, 0.20000000000000015, 0.20100000000000015, 0.20200000000000015, 0.20300000000000015, 0.20400000000000015, 0.20500000000000015, 0.20600000000000016, 0.20700000000000016, 0.20800000000000016, 0.20900000000000016, 0.21000000000000016, 0.21100000000000016, 0.21200000000000016, 0.21300000000000016, 0.21400000000000016, 0.21500000000000016, 0.21600000000000016, 0.21700000000000016, 0.21800000000000017, 0.21900000000000017, 0.22000000000000017, 0.22100000000000017, 0.22200000000000017, 0.22300000000000017, 0.22400000000000017, 0.22500000000000017, 0.22600000000000017, 0.22700000000000017, 0.22800000000000017, 0.22900000000000018, 0.23000000000000018, 0.23100000000000018, 0.23200000000000018, 0.23300000000000018, 0.23400000000000018, 0.23500000000000018, 0.23600000000000018, 0.23700000000000018, 0.23800000000000018, 0.23900000000000018, 0.24000000000000019, 0.2410000000000002, 0.2420000000000002, 0.2430000000000002, 0.2440000000000002, 0.2450000000000002, 0.2460000000000002, 0.2470000000000002, 0.2480000000000002, 0.2490000000000002, 0.25000000000000017, 0.25100000000000017, 0.25200000000000017, 0.25300000000000017, 0.25400000000000017, 0.25500000000000017, 0.25600000000000017, 0.2570000000000002, 0.2580000000000002, 0.2590000000000002, 0.2600000000000002, 0.2610000000000002, 0.2620000000000002, 0.2630000000000002, 0.2640000000000002, 0.2650000000000002, 0.2660000000000002, 0.2670000000000002, 0.2680000000000002, 0.2690000000000002, 0.2700000000000002, 0.2710000000000002, 0.2720000000000002, 0.2730000000000002, 0.2740000000000002, 0.2750000000000002, 0.2760000000000002, 0.2770000000000002, 0.2780000000000002, 0.2790000000000002, 0.2800000000000002, 0.2810000000000002, 0.2820000000000002, 0.2830000000000002, 0.2840000000000002, 0.2850000000000002, 0.2860000000000002, 0.2870000000000002, 0.2880000000000002, 0.2890000000000002, 0.2900000000000002, 0.2910000000000002, 0.2920000000000002, 0.2930000000000002, 0.2940000000000002, 0.2950000000000002, 0.2960000000000002, 0.2970000000000002, 0.2980000000000002, 0.2990000000000002, 0.3000000000000002, 0.3010000000000002, 0.3020000000000002, 0.3030000000000002, 0.3040000000000002, 0.3050000000000002, 0.3060000000000002, 0.3070000000000002, 0.3080000000000002, 0.3090000000000002, 0.3100000000000002, 0.3110000000000002, 0.3120000000000002, 0.3130000000000002, 0.3140000000000002, 0.3150000000000002, 0.3160000000000002, 0.3170000000000002, 0.3180000000000002, 0.31900000000000023, 0.32000000000000023, 0.32100000000000023, 0.32200000000000023, 0.32300000000000023, 0.32400000000000023, 0.32500000000000023, 0.32600000000000023, 0.32700000000000023, 0.32800000000000024, 0.32900000000000024, 0.33000000000000024, 0.33100000000000024, 0.33200000000000024, 0.33300000000000024, 0.33400000000000024, 0.33500000000000024, 0.33600000000000024, 0.33700000000000024, 0.33800000000000024, 0.33900000000000025, 0.34000000000000025, 0.34100000000000025, 0.34200000000000025, 0.34300000000000025, 0.34400000000000025, 0.34500000000000025, 0.34600000000000025, 0.34700000000000025, 0.34800000000000025, 0.34900000000000025, 0.35000000000000026, 0.35100000000000026, 0.35200000000000026, 0.35300000000000026, 0.35400000000000026, 0.35500000000000026, 0.35600000000000026, 0.35700000000000026, 0.35800000000000026, 0.35900000000000026, 0.36000000000000026, 0.36100000000000027, 0.36200000000000027, 0.36300000000000027, 0.36400000000000027, 0.36500000000000027, 0.36600000000000027, 0.36700000000000027, 0.36800000000000027, 0.36900000000000027, 0.3700000000000003, 0.3710000000000003, 0.3720000000000003, 0.3730000000000003, 0.3740000000000003, 0.3750000000000003, 0.3760000000000003, 0.3770000000000003, 0.3780000000000003, 0.3790000000000003, 0.3800000000000003, 0.3810000000000003, 0.3820000000000003, 0.3830000000000003, 0.3840000000000003, 0.3850000000000003, 0.3860000000000003, 0.3870000000000003, 0.3880000000000003, 0.3890000000000003, 0.3900000000000003, 0.3910000000000003, 0.3920000000000003, 0.3930000000000003, 0.3940000000000003, 0.3950000000000003, 0.3960000000000003, 0.3970000000000003, 0.3980000000000003, 0.3990000000000003, 0.4000000000000003, 0.4010000000000003, 0.4020000000000003, 0.4030000000000003, 0.4040000000000003, 0.4050000000000003, 0.4060000000000003, 0.4070000000000003, 0.4080000000000003, 0.4090000000000003, 0.4100000000000003, 0.4110000000000003, 0.4120000000000003, 0.4130000000000003, 0.4140000000000003, 0.4150000000000003, 0.4160000000000003, 0.4170000000000003, 0.4180000000000003, 0.4190000000000003, 0.4200000000000003, 0.4210000000000003, 0.4220000000000003, 0.4230000000000003, 0.4240000000000003, 0.4250000000000003, 0.4260000000000003, 0.4270000000000003, 0.4280000000000003, 0.4290000000000003, 0.4300000000000003, 0.4310000000000003, 0.43200000000000033, 0.43300000000000033, 0.43400000000000033, 0.43500000000000033, 0.43600000000000033, 0.43700000000000033, 0.43800000000000033, 0.43900000000000033, 0.44000000000000034, 0.44100000000000034, 0.44200000000000034, 0.44300000000000034, 0.44400000000000034, 0.44500000000000034, 0.44600000000000034, 0.44700000000000034, 0.44800000000000034, 0.44900000000000034, 0.45000000000000034, 0.45100000000000035, 0.45200000000000035, 0.45300000000000035, 0.45400000000000035, 0.45500000000000035, 0.45600000000000035, 0.45700000000000035, 0.45800000000000035, 0.45900000000000035, 0.46000000000000035, 0.46100000000000035, 0.46200000000000035, 0.46300000000000036, 0.46400000000000036, 0.46500000000000036, 0.46600000000000036, 0.46700000000000036, 0.46800000000000036, 0.46900000000000036, 0.47000000000000036, 0.47100000000000036, 0.47200000000000036, 0.47300000000000036, 0.47400000000000037, 0.47500000000000037, 0.47600000000000037, 0.47700000000000037, 0.47800000000000037, 0.47900000000000037, 0.48000000000000037, 0.48100000000000037, 0.4820000000000004, 0.4830000000000004, 0.4840000000000004, 0.4850000000000004, 0.4860000000000004, 0.4870000000000004, 0.4880000000000004, 0.4890000000000004, 0.4900000000000004, 0.4910000000000004, 0.4920000000000004, 0.4930000000000004, 0.4940000000000004, 0.4950000000000004, 0.4960000000000004, 0.4970000000000004, 0.4980000000000004]

以前的方法为什么不起作用?

  • t是一个长度为1的数组(它只有1个元素)
  • 在第一次迭代中,代码尝试访问t[1](即列表t中的第二个元素)
  • t没有第二个元素,因此引发了IndexError
  • t is an array of length 1 (it has only 1 element)
  • In the first iteration, the code tries to access t[1] (i.e the 2nd element in the list t)
  • t does not have a 2nd element, so it throws an IndexError

这篇关于为什么列表索引超出范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆