python - 在python中查找2个字符串之间的匹配表达式
问题描述
例如
string1 = "Hello this is the first string, it will be used in this project"
string2 = "Hello this is string2 and i will not be used in this project"
我希望这个脚本从 string2 返回 string1 中的所有匹配表达式
输出应该是:[Hello this is, will, be used in this project]
我已经制作了一个自定义函数来使用循环来实现这一点,但我担心它太慢了
这是我的自定义函数,我确信它在很多方面都很糟糕,但我仍在学习 python。
from re import search
fullstring = "Hello this is the first string, it will be used in this project"
substring = "Hello this is string2 and i will not be used in this project"
last_found_str = ""
#while string is not empty keep looping
while test_substring:
print("Searching for {}".format(test_substring))
#if substring in fullstring
if search(r'\b{}\b'.format(test_substring), fullstring):
print("Found! : {}".format(test_substring))
#add to list
phrases_list.append(test_substring)
#remove the found substring
substring = substring.replace(test_substring,'')
if substring == " ":
break
test_substring = substring
#this is the substring from the last found results
last_found_str = substring
else:
#if only one word is left
if len(test_substring.split()) == 1:
print("{} is not found".format(test_substring))
if len(last_found_str.split()) > 1:
#if substring from last found results is more than 1 word, remove the first word and loop through the rest
substring = substring.replace(r'\b{}\b'.format(test_substring),'')
test_substring = last_found_str.partition(' ')[2]
last_found_str = test_substring
else:
#if its a word or less then stop looping
break
else:
#if there is more than one word remove the last word and keep looping
test_substring = test_substring.rsplit(' ', 1)[0]
print(phrases_list)
解决方案
这是应该完成这项工作的列表理解:
[i for i in string1.split() if i in string2.split()]
输出:
['Hello', 'this', 'is', 'will', 'be', 'used', 'in', 'this', 'project']
推荐阅读
- python - 在没有诅咒的Python中替换控制台中的多行
- c++ - PkgConfig 模块:INCLUDE_DIRS 未列出所有目录
- java - 在 dockerized 环境中,spark worker 无法解析应用程序主机
- swift - 选择过滤的搜索结果
- vba - 对于范围内的每个单元格,如果单元格值不为空,则 vlookup
- android - Xamarin.Android 卡片视图高度未显示
- java - 使用 Java 应用程序时阻止访问应用程序
- javascript - 谷歌地图用标记显示最近的公园
- f# - F# WebApi Cors 不起作用
- linux - 使用 bash 将命令的每一行转储到不同的变量中