如何找到按任何顺序匹配子字符串的字符串?
假设列表如下:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从逻辑上讲,您的问题是比较单词中字符的 set ,以根据列表中每个单词中字符的 set 查找。如果后一个单词包含单词中的所有字符,则是匹配。这是一种使用列表理解的方法,以及set
intesection
:如果要保留任何输入字符串的空字符串占位符,以 not 匹配,请使用此版本:
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
:If you want to retain an empty string placeholder for any input string which does not match, then use this version:
您需要to_find的字母彼此相邻,还是所有字母都应该在单词中?基本上:
seabco
是否匹配?[您的问题不包括此细节,您经常使用“子字符串”,但也“因为它都有to_find中的所有字母”,所以我不确定如何解释它。]
如果
seabco
匹配,然后@Tim biegeleisen的答案是正确的。如果字母需要彼此相邻(但当然是任何顺序),请在下面查看:如果
to_find
相对较短,则可以仅生成字母的所有排列(>
中检查) n!
,所以在这里(3!)= 6:eos,eso,oes,ose,seo,soe)并在。
https://docs.python.orgg/3/library/itertools。 html#itertools.permutations
我们做
“”。加入(perm)
,因为perm是元组,我们需要一个字符串。不太明显但更好的复杂性是仅获取我们的字符串的3个字符 - 彼此相邻)并将它们设置为compare to_find。
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 checkin
.https://docs.python.org/3/library/itertools.html#itertools.permutations
We do
"".join(perm)
because perm is a tuple and we need a string.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.