开发者

python: list index of out of range

开发者 https://www.devze.com 2023-01-10 13:53 出处:网络
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)
  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号