在Python中使用交集查找多于1个单词

时间:2018-07-18 12:24:14

标签: python

我目前正在使用以下内容查找一个或多个单词是否在字符串中:

stopwords=set(["Good to Soft","Slow","Standard to Slow","Standard","Standard to Fast","Fast","Heavy","Soft to Heavy","Soft","Yeliding to Soft","Yeilding","Good to Yeilding","Good","Good to Firm","Firm","Hard"])
Going = stopwords.intersection(set(testtext[0].split()))

我的问题是,如果字符串中有Standard To Slow,那么交集似乎只会拾取StandardSlow-我想要Standard To Slow

反正我有什么可以改善的吗?

为了更清楚地说明我需要什么结果,我将使用以下示例:

我有一个活动清单

["Good to Soft","Slow","Standard to Slow","Standard","Standard to Fast","Fast","Heavy","Soft to Heavy","Soft","Yeliding to Soft","Yeilding","Good to Yeilding","Good","Good to Firm","Firm","Hard"]

我想找到这些字符串中的哪些,例如:

  

2:20-H布朗与儿子回收少女障碍赛(Class4)�4,094好   2m3f207y

这将返回“ good”,这就是匹配结果

  

2:20-H布朗与儿子回收少女障碍赛(Class4)�4,094对   软2m3f207y

这将返回“ Good to Soft”,即匹配,而不是“ Soft”

1 个答案:

答案 0 :(得分:0)

交集只会找到匹配的元素。这是您正在比较的列表:

["Good to Soft","Slow","Standard to Slow","Standard","Standard to Fast","Fast","Heavy","Soft to Heavy","Soft","Yeliding to Soft","Yeilding","Good to Yeilding","Good","Good to Firm","Firm","Hard"]

testtext[0].split() == ["Standard", "to", "Slow"]

如果只希望它与testtext[0]中的内容匹配,则不应使用split()创建列表:

Going = stopwords.intersection({testtext[0]})