[英]indexerror in python with beaglebone black
y[i] = ADC.read("P9_40")
IndexError: list assignment index out of range
the code: 编码:
i = 1
x = []*1000
y = []*1000
for i in range(1000):
y[i] = ADC.read("P9_40") # adc input pin
x[i] = (int)(y*2147483648) # conversion of float to int
this code is read data from analogue pin of beaglebone black and store the result in array 此代码是从beaglebone black的模拟引脚读取数据并将结果存储在数组中
In python you can't call a uncreated index of a list 在python中,您无法调用列表的未创建索引
x = []*1000 #creates only one empty list not 1000 list of list
y = []*1000 #like wise here
It should rather be like this 它应该像这样
x = [[] for i in range(1000)]
y = [[] for i in range(1000)]
You don't create a 1000 element list by doing: 您不会通过执行以下操作来创建1000个元素列表:
x = [] * 1000
y = [] * 1000
What you should do is create it with None
values: 您应该做的是使用
None
值创建它:
x = [None] * 1000
y = [None] * 1000
and then you can overwrite those None
in your loop. 然后您可以覆盖循环中的“
None
。 None
is better than any integer value (like 0
) that might come out of ADC.read()
, as you can check if the whole list is updated afterwards. None
什么比ADC.read()
可能产生的任何整数值(例如0
)更好的了,因为您可以检查整个列表是否在以后更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.