在Python中搜索包含某字符串的数组

7

我有一个字符串,如search = 'hello',需要检查整个数组,看是否包含hello
例如:

"hello" == "123hello123" | true
"hello" == "aasdasdasd123hello123" | true
"hello" == "123123hello" | true

我需要类似于代码搜索的东西

list_all_files_in_google_drive - 这是我的所有文件名数组

filename - 文件名,需要检查它是否存在于谷歌云端硬盘中

if not any(file['title'] == filename for file in list_all_files_in_google_drive):
   print('file not exist')

我的代码不能正常工作是因为它的工作方式如下:

"hello" == "123hello123" | false
"hello" == "aasdasdasd123hello123" | false
"hello" == "123123hello" | false
"hello" == "hello" | true

我需要它像这样工作:

我需要按照以下方式运行:

"hello" == "123hello123" | true
"hello" == "aasdasdasd123hello123" | true
"hello" == "123123hello" | true
"hello" == "hello" | true

更新:

我检查了运算符 in ,它没有输出true

filename = 'hello'
list = ['123hello123', 'aasdasdasd123hello123', '123123hello']
if filename in list:
    print('true')

1
尝试在“123hello123”中找到“hello”。 - Rakesh
2
但是您已经用“in”替换了整个循环。这不是Rakesh告诉您要做的事情。 - Daniel Roseman
5个回答

6

只需使用简单的循环遍历列表中的每个字符串,并使用Python成员in运算符检查是否存在'hello'

lst = ['123hello123', 'aasdasdasd123hello123', '123123hello']

for x in lst:
    if 'hello' in x:
        print('true')

这将输出:

true
true
true

或者,如果您想一次检查lst中的所有字符串:all()

if all('hello' in x for x in lst):
    print('true')

或者,如果您想一次检查lst中的所有字符串是否使用any()

if any('hello' in x for x in lst):
    print('true')

两个都会输出:

true

注意: 在您的问题中使用list作为变量名并不是个好主意,因为它会掩盖内置函数list()。此外,在这里返回布尔值TrueFalse就可以了,不需要返回这些的字符串形式。


2
为什么不使用 for l in lst: if 'hello' in l:?变量 filename 看起来有点多余。 - Poiz
@RoadRunner 这个例子完美地工作了,我使用 if any('hello' in x for x in lst): 但是也许你知道为什么这个语句对于 search_word = Taxi Stockholm Kostnad för resa 545 kr.pdf 和列表包含 20170906144217_2of3_556944-8102+FAKTURA_luisa@gluehome.com_Taxi Stockholm Kostnad för resa 545 kr.pdf 输出 false 吗? - r1299597

6

'in'替换'=='。

"hello" in "123hello123"            # RETURNS True
"hello" in "aasdasdasd123hello123"  # RETURNS True
"hello" in "123123hello"            # RETURNS True
"hello" in "hello"                  # RETURNS True

谢谢您的回答,但我尝试了您的示例,它没有起作用,请查看https://repl.it/repls/MintcreamDarkslategreyAustrianpinscher。 - r1299597
1
抱歉,我无法打开您提供的链接。您能在评论中发布您尝试过的内容吗? - Rakesh
2
如果在谷歌云盘中的所有文件列表中没有任何一个文件的标题与文件名匹配,则执行以下操作: 打印输出 'file not exist' 这就是如何使用'in'。 - Rakesh

1
您可以使用如下所示的列表推导式:

```

你可以使用以下代码:

```

filename = 'hello'
my_list = ['123hello123', 'aasdasdasd123hello123', '123123hello']

print all([True if filename in _ else False for _ in my_list])

filename = 'hello'
my_list = ['123hello123', 'aasdasdasd123hello123', '123123helo']

print all([True if filename in _ else False for _ in my_list])

output:

True
False

另一种解决方案是拥有一个自己的函数,如下所示:
def check_filename(filename_string, input_list):
    result = 'true'
    for _ in input_list:
        if filename_string not in _:
            result = 'false'
            break
    return result

filename = 'hello'
my_list = ['123hello123', 'aasdasdasd123hello123', '123123hello']

print(check_filename(filename_string=filename, input_list=my_list))

filename = 'hello'
my_list = ['123hello123', 'aasdasdasd123ho123', '123123hello']

print(check_filename(filename_string=filename, input_list=my_list))

output:

true
false

0

兄弟,试试这个:

filename = 'hello'
list = ['123hello123', 'aasdasdasd123hello123', '123123hello']
if (filename.find(file) for file in list):
      print('true')

filename = '555' list = ['123hello123', 'aasdasdasd123hello123', '123123hello'] if (filename.find(file) for file in list): print('true') else: print('false') it always output true - r1299597
1
将filename.find(file)更改为filename in file,然后尝试。 - Vikas Periyadath

0
'

in' 运算符是一个好的解决方案,您也可以尝试使用正则表达式:

'
import re
pattern=r'hello'
lst = ['123hello123', 'aasdasdasd123hello123', '123123hello','nothing']
for i in lst:
    if re.search(pattern,i):
        print("string : {} result : {} ".format(i,True))
    else:
        print("string : {} result : {} ".format(i,False))

输出:

string : 123hello123 result : True 
string : aasdasdasd123hello123 result : True 
string : 123123hello result : True 
string : nothing result : False 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接