如何找到按任何顺序匹配子字符串的字符串?

发布于 2025-01-29 12:42:58 字数 665 浏览 1 评论 0原文

假设列表如下:

list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']

和一个子字符串,

to_find = 'eos'

我想在匹配子字符串的list_of_strings中找到字符串(s)。来自list_of_strings的输出应为['SEO','paseo','oes'](因为它在to_find sub String)

我尝试了几件事:

a = next((string for string in list_of_strings if to_find in string), None) # gives NoneType object as output

&

result = [string for string in list_of_strings if to_find in string] # gives [] as output

但是这两个代码都不起作用。

有人可以告诉我我正在犯的错误吗?

谢谢

Assuming a list as follows:

list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']

and a sub string

to_find = 'eos'

I would like to find the string(s) in the list_of_strings that match the sub string. The output from the list_of_strings should be ['seo', 'paseo', 'oes'] (since it has all the letters in the to_find sub string)

I tried a couple of things:

a = next((string for string in list_of_strings if to_find in string), None) # gives NoneType object as output

&

result = [string for string in list_of_strings if to_find in string] # gives [] as output

but both the codes don't work.

Can someone please tell me what is the mistake I am doing?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福%小乖 2025-02-05 12:42:58

从逻辑上讲,您的问题是比较单词中字符的 set ,以根据列表中每个单词中字符的 set 查找。如果后一个单词包含单词中的所有字符,则是匹配。这是一种使用列表理解的方法,以及set intesection

list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']
to_find = 'eos'
to_find_set = set(list(to_find))
output = [x for x in list_of_strings if len(to_find_set.intersection(set(list(x)))) == len(to_find_set)]
print(output)  # ['seo', 'paseo', 'oes']

如果要保留任何输入字符串的空字符串占位符,以 not 匹配,请使用此版本:

output = [x if len(to_find_set.intersection(set(list(x)))) == len(to_find_set) else '' for x in list_of_strings]
print(output)  # ['', '', '', 'seo', 'paseo', 'oes']

Your problem logically is comparing the set of characters in the word to find against the set of characters in each word in the list. If the latter word contains all characters in the word to find, then it is a match. Here is one approach using a list comprehension along with set intesection:

list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']
to_find = 'eos'
to_find_set = set(list(to_find))
output = [x for x in list_of_strings if len(to_find_set.intersection(set(list(x)))) == len(to_find_set)]
print(output)  # ['seo', 'paseo', 'oes']

If you want to retain an empty string placeholder for any input string which does not match, then use this version:

output = [x if len(to_find_set.intersection(set(list(x)))) == len(to_find_set) else '' for x in list_of_strings]
print(output)  # ['', '', '', 'seo', 'paseo', 'oes']
故事还在继续 2025-02-05 12:42:58

您需要to_find的字母彼此相邻,还是所有字母都应该在单词中?基本上:seabco是否匹配?

[您的问题不包括此细节,您经常使用“子字符串”,但也“因为它都有to_find中的所有字母”,所以我不确定如何解释它。]

如果seabco匹配,然后@Tim biegeleisen的答案是正确的。如果字母需要彼此相邻(但当然是任何顺序),请在下面查看:


如果to_find相对较短,则可以仅生成字母的所有排列(> ) n!,所以在这里(3!)= 6:eos,eso,oes,ose,seo,soe)并在中检查

import itertools
list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']
to_find = 'eos'

result = [string for string in list_of_strings if any("".join(perm) in string for perm in itertools.permutations(to_find))]

https://docs.python.orgg/3/library/itertools。 html#itertools.permutations

我们做“”。加入(perm),因为perm是元组,我们需要一个字符串。

>>> result = [string for string in list_of_strings if any("".join(perm) in string for perm in itertools.permutations(to_find))]
>>> result
['seo', 'paseo', 'oes']

不太明显但更好的复杂性是仅获取我们的字符串的3个字符 - 彼此相邻)并将它们设置为compare to_find。

list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']
to_find = 'eos'

result = [string for string in list_of_strings if any(set(three_substring)==set(to_find) for three_substring in zip(string, string[1:], string[2:]))]

Do you need the letters of to_find to be next to each other or just all the letters should be in the word? Basically: does seabco match or not?

[Your question does not include this detail and you use "substring" a lot but also "since it has all the letters in the to_find", so I'm not sure how to interpret it.]

If seabco matches, then @Tim Biegeleisen's answer is the correct one. If the letters need to be next to each other (but in any order, of course), then look below:


If the to_find is relatively short, you can just generate all permutations of letters (n! of them, so here (3!) = 6: eos, eso, oes, ose, seo, soe) and check in.

import itertools
list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']
to_find = 'eos'

result = [string for string in list_of_strings if any("".join(perm) in string for perm in itertools.permutations(to_find))]

https://docs.python.org/3/library/itertools.html#itertools.permutations

We do "".join(perm) because perm is a tuple and we need a string.

>>> result = [string for string in list_of_strings if any("".join(perm) in string for perm in itertools.permutations(to_find))]
>>> result
['seo', 'paseo', 'oes']

Less-obvious but better complexity would be to just get 3-character substrings of our strings (to keep them next to each other) and set-compare them to set of to_find.

list_of_strings = ['foo', 'bar', 'soap', 'seo', 'paseo', 'oes']
to_find = 'eos'

result = [string for string in list_of_strings if any(set(three_substring)==set(to_find) for three_substring in zip(string, string[1:], string[2:]))]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文