gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:19:13 25 4
gpt4 key购买 nike

我有一个包含列表 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])=2,所以j应该只循环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

最佳答案

线

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

这不是看起来更漂亮、更有意义吗?

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

关于python for循环遍历不在指定范围内的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30082349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com