[英]Check intersection between two strings in python
I'm trying to check intersection between two strings using Python. 我正在尝试使用Python检查两个字符串之间的交集。 I defined this function:
我定义了这个功能:
def check(s1,s2):
word_array = set.intersection(set(s1.split(" ")), set(s2.split(" ")))
n_of_words = len(word_array)
return n_of_words
It works with some sample string, but in this specific case: 它适用于一些示例字符串,但在这种特定情况下:
d_word = "BANGKOKThailand"
nlp_word = "Despite Concerns BANGKOK"
print(check(d_word,nlp_word))
I got 0. What am I missing? 我得到0。我想念什么?
设置一包含单个字符串,设置两个包含三个字符串,并且字符串"BANGKOKThailand"
不等于字符串"BANGKOK"
。
I can see two might-be mistakes: 我可以看到两个可能的错误:
n_of_words = len(array)
should be 应该
n_of_words = len(word_array)
and 和
d_word = "BANGKOKThailand"
is missing a space in-between as 缺少之间的空格
"BANGKOK Thailand"
Fixing those two changes gave me a result of 1. 修正这两个更改后,结果为1。
I was looking for the maximum common part of 2 strings no matter where this part would be. 我一直在寻找2个字符串的最大公共部分,无论这部分在哪里。
def get_intersection(s1, s2):
res = ''
l_s1 = len(s1)
for i in range(l_s1):
for j in range(i + 1, l_s1):
t = s1[i:j]
if t in s2 and len(t) > len(res):
res = t
return res
#get_intersection(s1, s2)
Works for this example as well: 同样适用于此示例:
>>> s1 = "BANGKOKThailand"
>>> s2 = "Despite Concerns BANGKOK"
>>> get_intersection('aa' + s1 + 'bb', 'cc' + s2 + 'dd')
'BANGKOK'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.