Python比较文本

问题描述 投票:0回答:1

我想编写一个代码,在两个文本之间进行比较,并告诉我一些相同的字符(或某些相同的单词)我怎样才能做到这一点?我不想使用:

print(text1 == text2)

i除了类似的东西:

a = "i from israel"
b = "hello i from london"

# *i* and *from* are in both strings = 2

c = "apple orange banana watermelon"
d = "apple is very healthy, also banana and orange"

# *apple* and *banana* and *orange* are in both strings = 3

我想比较低沉重度,我的意思是,计算两个字符串中的单词。谢谢

python-3.x string compare
1个回答
0
投票

您可以通过执行以下操作获得相同的单词:list(set(a.split(' ')) & set(b.split(' ')))

感谢this回答。

© www.soinside.com 2019 - 2024. All rights reserved.