简体   繁体   中英

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

In python you can't call a uncreated index of a list

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:

x = [] * 1000
y = [] * 1000

What you should do is create it with None values:

x = [None] * 1000
y = [None] * 1000

and then you can overwrite those None in your loop. 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.

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