Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从2个字符串中查找等价词_Python_String - Fatal编程技术网

Python 从2个字符串中查找等价词

Python 从2个字符串中查找等价词,python,string,Python,String,我有两条弦“我晚上上学打羽毛球”和“我晚上去e2和r2”。如何找到e2和r2打羽毛球。我尝试使用for循环,但正在寻找一种更优雅的方法 a = set('I go to school and play badminton in the evening'.split(' ')) b = set('I go to and in the evening'.split(' ')) print(a - b) >>> {'badminton', 'school', 'play'} 为您

我有两条弦“我晚上上学打羽毛球”和“我晚上去e2和r2”。如何找到e2和r2打羽毛球。我尝试使用for循环,但正在寻找一种更优雅的方法

a = set('I go to school and play badminton in the evening'.split(' '))
b = set('I go to and in the evening'.split(' '))

print(a - b)
>>> {'badminton', 'school', 'play'}
为您的编辑编辑:


如果您还想直接从解析中命名它们,那么您需要稍微调整输入,可能还需要使用:

,因为模式可能会随着字符串而每次更改,这将成为一个字符串比较问题。使用
difflib
可以提供非常优雅的解决方案

str1 = "I go to e2 and r2 in the evening"
str2 = "I go to school and play badminton in the evening"

from difflib import SequenceMatcher
s = SequenceMatcher(None, str1, str2)
diff = [(str1[opcode[1]:opcode[2]], str2[opcode[3]:opcode[4]])
        for opcode in s.get_opcodes() if opcode[0] == 'replace']
print(diff)
# [('e2', 'school'), ('r2', 'play badminton')]
以前的解决方案:
我认为在这种情况下,最恰当和灵活的方法是使用正则表达式搜索

import re
pattern = "I go to (.*) and (.*) in the evening"
string = "I go to school and play badminton in the evening"
m = re.match(pattern, string)
e2 = m.groups()[0]
r2 = m.groups()[1]
result = e2 == 'school' and r2 == 'play badminton'
print(result)

问题已经改变了。@我只是想知道如何进一步编辑我的答案:1.你能操纵输入吗?2.名字重要吗?请再读一遍问题。您一定没有阅读最近的编辑。@我确实阅读了您的编辑,但我想进一步澄清,以帮助您无法操作输入。名字并不重要,模式可能会随着字符串的变化而改变。我回答了你的问题。你的问题中没有具体说明模式可能如何变化。请澄清或举例说明。我想我现在明白你的意思了。像‘e2’或‘r2’之类的短语或其他明显出现在不同地方的文本中-对吗?如果字符串是‘我晚上去上学、游泳和打羽毛球’?是的,这是可能的。在这种情况下,e2=学校,r2=游泳和打羽毛球为什么r2会包括“晚上”呢?为什么e2不包括“和游泳”呢?这回答了你的问题吗?请不要只发布代码作为答案,还要解释代码的作用以及它是如何解决问题的。带有解释的答案通常更有帮助,质量更好,更容易吸引选票。
import re
pattern = "I go to (.*) and (.*) in the evening"
string = "I go to school and play badminton in the evening"
m = re.match(pattern, string)
e2 = m.groups()[0]
r2 = m.groups()[1]
result = e2 == 'school' and r2 == 'play badminton'
print(result)