我有一个名为txtFreeForm的字符串列表:
['Add roth Sweep non vested money after 5 years of termination',
'Add roth in-plan to the 401k plan.]
我需要检查句子中是否只有Add roth。为此我用了这个
for each_line in txtFreeForm:
match = re.search('add roth',each_line.lower())
if match is not None:
print(each_line)
但这显然会返回我列表中的两个字符串,因为它们都包含“add roth”。有没有一种方法可以在一个句子中专门搜索“Add roth”,因为我要在字符串中搜索这些模式。你知道吗
谢谢你的帮助!你知道吗
你能用字符串的.Length属性来解决这个问题吗?我不是一个经验丰富的Python程序员,但我认为它应该如何工作:
基本上,如果文本在字符串中,并且字符串的长度正好等于字符串“Add Roth”的长度,那么它只能包含“Add Roth”。你知道吗
我希望这是有帮助的。你知道吗
编辑:
我误解了你的要求。你想打印出包含“Add Roth”的句子,而不是包含“Add Roth in plan”的句子。是这样吗?你知道吗
这个代码怎么样?你知道吗
这似乎可以解决问题。通过搜索字符串并将其添加到比较中,可以排除任何字符串(如“in plan”)。你知道吗
你很接近:)试试这个:
编辑: 我误读了。。。你有很多这样的。这需要一些更具侵略性的魔法。你知道吗
为了解释一下正在发生的事情,你提到你有很多话要处理。比较两组项的传统方法是嵌套for循环。所以
我正在使用一些不同的技术来尝试实现一个“更平坦”的实现,并避免嵌套循环。我会尽力描述他们。你知道吗
相关问题 更多 >
编程相关推荐