简体   繁体   中英

python: list index of out of range

  for row in c:
    c1.append(row[0:13])

  for row in c1:
    row.append(float(row[13])/100)
    row.append(float(row[12])/float(row[13])/100)
    row.append(math.log10(float(row[12])))

c contains a csv file with many rows and columns c1 is a subset of c containing only the first 14 elements

i am getting IndexError: list index out of range on row.append(float(row[13])/100)

does anyone know what i am doing wrong?

The rows in c1 don't actually contain 14 elements, they contain 13.

The second index in a slice is non-inclusive. When you append row[0:13] to c1 you are appending from element 0 to the element before 13. Hence, there are only 13 elements.

This is why you get IndexError: list index out of range on row.append(float(row[13])/100) . row[13] is an attempt to access a non-existent 14th element.

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