简体   繁体   English

为什么这会给我IndexError?

[英]Why does this give me an IndexError?

I have the following code that opens a csv , and appends all the values to a list. 我有以下代码打开一个csv ,并将所有值附加到列表中。 I then remove all the values that do not start with '2' . 然后,我删除所有不以'2'开头的值。 However, on the line if lst[k][0] != '2': , it raises an error: 但是, if lst[k][0] != '2':则会引发错误:

Traceback (most recent call last):
  File "historical_tempo1.py", line 23, in <module>
    if lst[k][0] != '2':
IndexError: list index out of range

Here is the code: 这是代码:

y = open('today.csv')
lst = []
for k in y:
        lst.append(k)

lst = ' '.join(lst).split()

for k in range(0, len(lst)-1):
        if lst[k][0] != '2':
                lst[k:k+1] = ''

Here is the first bit of content from the csv file: 这是csv文件中内容的第一部分:

Date,Time,PM2.5 Mass concentration(ug/m3),Status

3/15/2014,4:49:13 PM,START
2014/03/15,16:49,0.5,0
3/15/2014,4:49:45 PM,START
2014/03/15,16:50,5.3,0
2014/03/15,16:51,5.1,0
2014/03/15,16:52,5.0,0
2014/03/15,16:53,5.0,0
2014/03/15,16:54,5.4,0
2014/03/15,16:55,6.4,0
2014/03/15,16:56,6.4,0
2014/03/15,16:57,5.0,0
2014/03/15,16:58,5.2,0
2014/03/15,16:59,5.2,0
3/15/2014,5:03:48 PM,START
2014/03/15,17:04,4.8,0
2014/03/15,17:05,4.9,0
2014/03/15,17:06,4.9,0
2014/03/15,17:07,5.1,0
2014/03/15,17:08,4.6,0
2014/03/15,17:09,4.9,0
2014/03/15,17:10,4.4,0
2014/03/15,17:11,5.7,0
2014/03/15,17:12,4.4,0
2014/03/15,17:13,4.0,0
2014/03/15,17:14,4.6,0
2014/03/15,17:15,4.7,0
2014/03/15,17:16,4.8,0
2014/03/15,17:17,4.5,0
2014/03/15,17:18,4.4,0
2014/03/15,17:19,4.5,0
2014/03/15,17:20,4.8,0
2014/03/15,17:21,4.6,0
2014/03/15,17:22,5.1,0
2014/03/15,17:23,4.2,0
2014/03/15,17:24,4.6,0
2014/03/15,17:25,4.5,0
2014/03/15,17:26,4.4,0
  1. Why do you get an IndexError ? 为什么会出现IndexError Because when you write lst[k:k+1] = '' , you have just removed the k+1 element from your list, which means your list is shorter by 1 element, and your loop is still going up to the old len(lst) , so the index variable k is guaranteed to go over. 因为当您编写lst[k:k+1] = '' ,您刚刚从列表中删除了k+1元素,这意味着您的列表短了1元素,并且循环仍旧沿旧len(lst) ,因此保证索引变量k可以通过。

  2. How can you fix this? 您该如何解决? Loop over a copy and delete from the original using list.remove() . 循环复制并使用list.remove()从原始副本中删除。

The following code loops over the copy. 以下代码循环遍历副本。

for s in lst[:]:
     if k[0] != '2':
         list.remove(k)

The expressions lst[k][0] raises an IndexError , which means that either: 表达式lst[k][0]引发IndexError ,这意味着:

# (1) this expressions raises it
x = lst[k]
# or (2) this expression raises it
x[0]

If (1) raises it, it means len(lst) <= k , ie there are fewer items than you expect. 如果(1)增大它,则意味着len(lst) <= k ,即项目少于您的预期。

If (2) raises it, it means x is an empty string, which means you can't access its item at index 0. 如果(2)引发它,则意味着x是一个空字符串,这意味着您无法在索引0处访问其项目。


Either way, instead of guessing, use pdb . 无论哪种方式,都不要使用pdb来猜测。 Run your program using pdb , and at the point your script aborts, examine the values of lst , k , lst[k] , and lst[k][0] . 使用pdb运行程序,然后脚本中止,请检查lstklst[k]lst[k][0]

Basically, your list, 'lst', starts out at length 43. The 'slice' operation lst[k:k+1] doesn't replace two separate indexed values with '', but wipes out one of the list entries. 基本上,列表'lst'从长度43开始。'slice'操作lst [k:k + 1]不会用''替换两个单独的索引值,而是清除了列表项之一。 If you did a lst[k:k+5], you would wipe out five entries. 如果您执行lst [k:k + 5],则将清除五个条目。 Try it in the interpreter. 在解释器中尝试。

I'd recommend you don't try to wipe out those entries particularly in the list you are performing operations. 我建议您不要尝试清除这些条目,尤其是要执行操作的列表中的条目。 It is shrinking in this case which means you go out of range and get an "IndexError". 在这种情况下,它正在缩小,这意味着您超出范围并得到“ IndexError”。 Store the values you want into another a list if you have to remove the lines that don't begin with "2". 如果您必须删除不以“ 2”开头的行,请将所需的值存储到另一个列表中。

List comprehensions work great in this case... 在这种情况下,列表理解非常有用。

mynewlist = [x for x in lst if x[0] == '2']

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

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