英文:
If else condition is not working from second iteration onwards in python
问题
你的代码中存在一个问题。在每次迭代中,你都重新定义了 key_text1
, key_text2
, key_text3
, 和 key_text4
,但在第二次迭代时,这些变量仍然包含第一次迭代的值。这就是为什么第二次迭代中会出现问题。你可以通过在每次迭代开始前将这些变量重置为 None
来解决这个问题。下面是修复后的代码:
import re
x = {'text1': 'whatever text may or may not come here KEYWORD yada yada yada whatever blah',
'text2': 'this is also KEYWORD another text that might contain yada yada yada blah'}
for k, v in x.items():
key_text1 = None # 重置变量
key_text2 = None
key_text3 = None
key_text4 = None
match1 = re.search(r"regex_pattern", v, flags=re.IGNORECASE)
match2 = re.search(r"regex_pattern", v, flags=re.IGNORECASE)
match3 = re.search(r"regex_pattern", v, flags=re.IGNORECASE)
match4 = re.search(r"regex_pattern", v, flags=re.IGNORECASE)
if match1:
print('match1 found for ', k)
key_text1 = v[match1.end():]
elif match2:
print('match2 found for ', k)
key_text2 = v[match2.end():]
elif match3:
print('match3 found for ', k)
key_text3 = v[match3.end():]
elif match4:
print('match4 found for ', k)
key_text4 = v[match4.end():]
else:
print('None of the above matches! found for ', k)
if key_text1:
x[k] = key_text1
elif key_text2:
x[k] = key_text2
elif key_text3:
x[k] = key_text3
elif key_text4:
x[k] = key_text4
else:
print('nothing!')
print(x)
这将确保在每次迭代中,key_text1
, key_text2
, key_text3
, 和 key_text4
都被重置为 None
,从而避免了在第二次迭代中的问题。
英文:
I am using regex to find whether a keyword is present in a text or not. If it is present, I am extracting all the text AFTER that keyword.
I have a dictionary where I am storing the text as a value. I am iterating through the dictionary values and am using regex to search whether the keyword is present in the text and if yes then extract all the text AFTER that keyword and update the value for that particular key. The issue is that it is not working for the second key value pair. Below is my code:
x = {'text1' : 'whatever text may or may not come here KEYWORD yada yada yada whatever blah',
'text2' : 'this is also KEYWORD another text that might contain yada yada yada blah'}
for k, v in x.items():
match1 = re.search(r"regex_pattern", v, flags = re.IGNORECASE)
match2 = re.search(r"regex_pattern", v, flags = re.IGNORECASE)
match3 = re.search(r"regex_pattern", v, flags = re.IGNORECASE)
match4 = re.search(r"regex_pattern", v, flags = re.IGNORECASE)
if match1:
print('match1 found for ', k)
key_text1 = v[match1.end():]
elif match2:
print('match2 found for ', k)
key_text2 = v[match2.end():]
elif match3:
print('match3 found for ', k)
key_text3 = v[match3.end():]
elif match4:
print('match4 found for ', k)
key_text4 = v[match4.end():]
else:
print('None of the above matches! found for ', k)
if key_text1:
x[k] = key_text1
elif key_text2:
x[k] = key_text2
elif key_text3:
x[k] = key_text3
elif key_text4:
x[k] = key_text4
else:
print('nothing!')
The output should look like the following:
x = {'text1' : ' yada yada yada whatever blah', 'text2' : ' another text that might contain yada yada yada blah'}
But for some reason it is not working for the second iteration. I am getting output like this:
x = {'text1' : ' yada yada yada whatever blah', 'text2' : ' yada yada yada whatever blah'}
In the second iteration,it is taking the first value and setting it as the value for the second key. I suspect the issue is that while iterating for the second key value pair, when it encounters the if condition i.e. if key_text1:
it was True from the first iteration and hence the first if condition is satisfied. So even if for second value the first condition is not True, since for the first value the condition was True it is taking the first condition to be True for the second value and proeeding.
How can I resolve this?
答案1
得分: 0
你的问题确实是在第二次循环中key_text1
仍然有效。处理这个问题的最简单方法是在if/else
中将赋值还原为x[k]
,而不是使用key_text*
变量:
if match1:
print('match1 found for ', k)
x[k] = v[match1.end():]
elif match2:
print('match2 found for ', k)
x[k] = v[match2.end():]
elif match3:
print('match3 found for ', k)
x[k] = v[match3.end():]
elif match4:
print('match4 found for ', k)
x[k] = v[match4.end():]
else:
print('None of the above matches! found for ', k)
请注意,我还建议将你的正则表达式放在一个列表中,并对其进行迭代,以进一步简化代码。例如:
x = {
'text1': 'whatever text may or may not come here KEYWORD yada yada yada whatever blah',
'text2': 'this is also KEYWORD2 another text that might contain yada yada yada blah',
'text3': 'some text without a key word'
}
regexes = [r'\bKEYWORD\s+', r'\bKEYWORD1\s+', r'\bKEYWORD2\s+']
for k, v in x.items():
for regex in regexes:
match = re.search(regex, v, flags=re.IGNORECASE)
if match:
x[k] = v[match.end():]
break
else:
print('No match found for ', k)
这将输出 No match found for text3
并且 x
的值将会更改为:
{
'text1': 'yada yada yada whatever blah',
'text2': 'another text that might contain yada yada yada blah',
'text3': 'some text without a key word'
}
英文:
Your problem is indeed that key_text1
is still valid on your second pass through the loop. The easiest way to deal with this is to make the assigment back to x[k]
in the if/else
instead of the key_text*
variables:
if match1:
print('match1 found for ', k)
x[k] = v[match1.end():]
elif match2:
print('match2 found for ', k)
x[k] = v[match2.end():]
elif match3:
print('match3 found for ', k)
x[k] = v[match3.end():]
elif match4:
print('match4 found for ', k)
x[k] = v[match4.end():]
else:
print('None of the above matches! found for ', k)
Note I would also put your regex's in a list and iterate over that to further simplify the code. For example:
x = {
'text1': 'whatever text may or may not come here KEYWORD yada yada yada whatever blah',
'text2': 'this is also KEYWORD2 another text that might conttin yada yada yada blah',
'text3': 'some text without a key word'
}
regexes = [r'\bKEYWORD\s+', r'\bKEYWORD1\s+', r'\bKEYWORD2\s+']
for k, v in x.items():
for regex in regexes:
match = re.search(regex, v, flags = re.IGNORECASE)
if match:
x[k] = v[match.end():]
break
else:
print('No match found for ', k)
This will output No match found for text3
and x
will change to:
{
'text1': 'yada yada yada whatever blah',
'text2': 'another text that might conttin yada yada yada blah',
'text3': 'some text without a key word'
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论