【问题标题】:Find the number of words that occur in both strings找出两个字符串中出现的单词数
【发布时间】:2020-05-13 05:05:57
【问题描述】:

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

print(text1 == text2)

我除了:

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 python-textprocessing


【解决方案1】:

您可以通过这样做获得相同的单词:list(set(a.split(' ')) & set(b.split(' ')))

感谢this的回答。

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 2017-12-17
    • 2010-12-28
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多