Python 检查子字符串是否在字符串列表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16380326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if substring is in a list of strings?
提问by álvaro
I have found some answers to this question before, but they seem to be obsolete for the current Python versions (or at least they don't work for me).
我之前已经找到了这个问题的一些答案,但它们对于当前的 Python 版本似乎已经过时(或者至少它们对我不起作用)。
I want to check if a substring is contained in a list of strings. I only need the boolean result.
我想检查一个子字符串是否包含在字符串列表中。我只需要布尔结果。
I found this solution:
我找到了这个解决方案:
word_to_check = 'or'
wordlist = ['yellow','orange','red']
result = any(word_to_check in word for word in worldlist)
From this code I would expect to get a Truevalue. If the word was "der", then the output should be False.
从这段代码中,我希望得到一个True值。如果单词是“der”,那么输出应该是False.
However, the result is a generator function, and I can't find a way to get the Truevalue.
但是,结果是生成器函数,我找不到获取True值的方法。
Any idea?
任何的想法?
采纳答案by Ashwini Chaudhary
You can import anyfrom __builtin__in case it was replaced by some other any:
您可以导入anyfrom__builtin__以防它被其他一些替换any:
>>> from __builtin__ import any as b_any
>>> lst = ['yellow', 'orange', 'red']
>>> word = "or"
>>> b_any(word in x for x in lst)
True
Note that in Python 3 __builtin__has been renamed to builtins.
请注意,在 Python 3__builtin__中已重命名为builtins.
回答by stderr
You could use nextinstead:
你可以next改用:
colors = ['yellow', 'orange', 'red']
search = "or"
result = next((True for color in colors if search in color), False)
print(result) # True
To show the string that contains the substring:
要显示包含子字符串的字符串:
colors = ['yellow', 'orange', 'red']
search = "or"
result = [color for color in colors if search in color]
print(result) # Orange
回答by Raymond Hettinger
The code you posted using any()is correct and should work unless you've redefined it somewhere.
您使用any()发布的代码是正确的并且应该可以工作,除非您在某处重新定义了它。
That said, there is a simple and fast solution to be had by using the substring search on a single combined string:
也就是说,通过在单个组合字符串上使用子字符串搜索,有一个简单而快速的解决方案:
>>> wordlist = ['yellow','orange','red']
>>> combined = '\t'.join(wordlist)
>>> 'or' in combined
True
>>> 'der' in combined
False
This should work muchfaster than the approach using any. The join character can be any character that doesn't occur in one of the words in the wordlist.
这应该工作多快于使用方法的任何。连接字符可以是不出现在词表中的一个词中的任何字符。
回答by Kostas Demiris
Also if someone wants to check if any of the values of a dictionary exists as a substring in a list of strings, can use this:
此外,如果有人想检查字典的任何值是否作为字符串列表中的子字符串存在,可以使用以下命令:
list_a = [
'Copy of snap-009ecf9feb43d902b from us-west-2',
'Copy of snap-0fe999422014504b6 from us-west-2',
'Copy of snap-0fe999422014cscx504b6 from us-west-2',
'Copy of snap-0fe999422sdad014504b6 from us-west-2'
]
dict_b = {
'/dev/xvda': 'snap-0fe999422014504b6',
'/dev/xvdsdsa': 'snap-sdvcsdvsdvs'
}
for b1 in dict_b.itervalues():
result = next( ("found" for a1 in a if b1 in a1), "not found")
print result
It prints
它打印
not found
found

