Python for looping through a value not in a specified range
I have python code that contains a list dict
where each item is a list of arbitrary size. I iterate over the elements like this:
for i in range(len(dict)):
for j in range(1,len(dict[i])):
str = dict[i][j]
I am getting an error IndexError: list index out of range
. I am using range(1,len(dict))
because I want to loop through the first element of each list in dict
. At the point generated error i=5
, len(dict[5])=2
so j
just to be sort out 1
, but when I check the value j
I receive 2
. How is this possible?
What's even weirder, when I type the above code in the python console, I don't get any such error and everything works fine.
Edit: complete code: (note the change from dict
to keywords
)
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
The actual lists are conds
both keywords
much longer and read from a file, but I just copied and pasted two items from each python console.
Edit 2: print i
, j
, len(dict[i])
, dict[i]
in the inner loop. The output is too long to be entered here, but here's a condensed version:
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
source to share
Line
ind = [i for i, x in enumerate(match) if x]
changes yours i
which you are using for the outer loop.
This btw wouldn't happen if you were using a regular Python loop instead of an indexed loop:
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
Doesn't that look much better and more meaningful?
Variable names could be further improved, but I'll leave that up to you as I don't know proper names for your things.
source to share