简体   繁体   English

如果字符串包含列表项

[英]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.

相关问题 检查字符串是否包含列表项 - Check if string contains list item 检查 Python 列表项是否包含另一个列表中的字符串 - Check if a Python list item contains a string inside another list 在Python中提取一个包含字符串的列表项,而无需列表理解? - Extract a list item in Python that contains a string, without list comprehension? 检查 Python 列表项是否在另一个字符串中包含一个字符串但有条件 - Check if a Python list item contains a string inside another string but with conditions Python-搜索包含字符串的列表项(区分大小写) - Python - Search a list item that contains a string (match case) 如果来自另一个对象的字符串包含子字符串,则从列表中删除项目 - Remove item from list if a string from another object contains substring 当项目包含方括号时,将大字符串转换为浮点数列表 - Convert a large string into a list of floats when an item contains brackets 如果列表包含一个项目,则列表等于项目 - If list contains an item, then list equals to item 清理数据:如何遍历列表查找项目是否包含字符串,空格或空格,然后在Python中删除该项目 - Cleaning data: How to iterate through a list find if item contains a string, whitespace or blank and delete that item in Python 检查列表中的项目是否包含数字 - Check if item in list contains a number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM