繁体   English   中英

如果字符串包含列表项

[英]If string contains an list item

我正在寻找一种检查字符串是否在列表中任何位置的方法

test = ['he', 'she', 'them']
string = 'hello'
if any(test in s for s in string):
    print 'yes'

我尝试这个,并且因为“他”在“你好”中,所以应该打印是,但是我得到了

TypeError: 'in <string>' requires string as left operand, not list

有什么帮助吗?

代替迭代字符串对象,迭代列表对象test

并检查测试项目是否在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM