[英]If string contains an list item
I'm looking for a way to check if an string contains anywhere in it, a list item 我正在寻找一种检查字符串是否在列表中任何位置的方法
test = ['he', 'she', 'them']
string = 'hello'
if any(test in s for s in string):
print 'yes'
I try this, and since 'he' is in 'hello' it should print yes, but i get 我尝试这个,并且因为“他”在“你好”中,所以应该打印是,但是我得到了
TypeError: 'in <string>' requires string as left operand, not list
any help? 有什么帮助吗?
Instead of iterating a string object, iterate the list object test
. 代替迭代字符串对象,迭代列表对象
test
。
And check whether the test item is in string
: 并检查测试项目是否在
string
:
test = ['he', 'she', 'them']
string = 'hello'
if any(test_item in string for test_item in test):
print 'yes'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.