python for循环遍历值不在指定的范围内

时间:2015-05-06 16:24:08

标签: python python-2.7

我有一个包含列表dict的python代码,其中每个元素都是任意大小的列表。我循环遍历元素如下:

for i in range(len(dict)):
    for j in range(1,len(dict[i])):
        str = dict[i][j]

此时,我收到错误IndexError: list index out of range。我正在使用range(1,len(dict)),因为我想跳过dict中每个列表的第一个元素。在生成错误时,i=5len(dict[5])=2j应仅循环1,但当我检查j值时,我得到2。这怎么可能?

甚至更奇怪的是,当我在python控制台中输入上面的代码时,我没有得到这样的错误,一切正常。

修改:完整代码为:(请注意从dictkeywords的更改)

import re

conds = [['emerald cryo i&ii,a,01', '40% (v/v) mpd', 'sodium phosphate dibasic', 'citric acid'],['emerald cryo i&ii,a,02', '40% (v/v) ethylene glycol', 'sodium acetate', 'acetic acid'],['emerald cryo i&ii,a,03', '50% (v/v) peg-200', 'citrate', 'na']]

keywords = [["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"],["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]]

#cycle through elements to see if there is a match to the dictionary
for i in range(len(keywords)):
    for j in range(1,len(keywords[i])):
        print j
        for k in range(len(conds)):
            str = keywords[i][j].strip().strip("'").strip() #this is where the error occurs
            match = [(str == l) for l in conds[k]]
            ind = [i for i, x in enumerate(match) if x]
            if len(ind) !=0:
                print ind
                print str

实际的condskeywords列表要长得多,要从文件读入,但我只是从python控制台复制并粘贴了两个元素。

编辑2:在内部循环中打印出ijlen(dict[i])dict[i]。输出太长了,不能放在这里,但这是一个浓缩版本:

0 1 3 ["'potassium acetate'", " 'k(oac)'", " 'potassium acetate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
5 1 2 ["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
3 1 3 ["'ammonium nitrate'", " '(nh4)2(no)3'", " 'ammonium nitrate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
[2]
sodium acetate
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na acetate'", " 'na_acetate'"]
4 1 5 ["'sodium acetate'", " 'sodium acetate'", " 'na(ac)'", " 'na     acetate'", " 'na_acetate'"]
5 1 2 ["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]

...

5 2 2 ["'rbcl+mgcl2'", " 'rbcl+mgcl2 (0.025m each)'"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "coarseCondEdit.py", line 38, in <module>
    str = keywords[i][j].strip().strip("'").strip()
IndexError: list index out of range

1 个答案:

答案 0 :(得分:5)

该行

ind = [i for i, x in enumerate(match) if x]

更改您用于外循环的i

如果您使用普通的Python循环而不是索引循环,那顺便说一句也不会发生:

for words in keywords:
    for word in words[1:]:
        for cond in conds:
            word = word.strip().strip("'").strip()
            match = [(word == l) for l in cond]
            ind = [i for i, x in enumerate(match) if x]
            if len(ind) !=0:
                print ind
                print word

这看起来更好,更有意义吗?

变量名称可以进一步改进,但我会把它留给你,因为我不知道你的事物的名称。