简体   繁体   English

python:列表索引超出范围

[英]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 c包含具有许多行和列的csv文件c1c的子集,仅包含前14个元素

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

does anyone know what i am doing wrong? 有人知道我在做什么错吗?

The rows in c1 don't actually contain 14 elements, they contain 13. c1中的行实际上并不包含14个元素,而是包含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. 当您将row[0:13]附加到c1您是将元素0附加到13 之前的元素。因此,只有13个元素。

This is why you get IndexError: list index out of range on row.append(float(row[13])/100) . 这就是为什么出现IndexError: list index out of range row.append(float(row[13])/100)上的IndexError: list index out of range row[13] is an attempt to access a non-existent 14th element. row[13]试图访问不存在的第14个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM