繁体   English   中英

search包含数组python中的字符串

[英]search contains string in array python

我有一个类似于search = 'hello'的字符串,我需要检查所有数组以查看是否包含hello
例如:

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

我需要搜索我的代码

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

filename - 名称文件,需要检查是否存在于google驱动器中

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

UPD:

我检查了运营商in和它不输出true

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

将'=='替换为'in'

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

刚刚经历与一个简单的循环列表中的每个字符串,并检查是否'hello'与蟒蛇成员存在in运营商:

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

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

哪个输出:

true
true
true

或者如果你想一次检查all() lst中的字符串:

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很好,不需要返回这些字符串形式。

试试这个家伙:

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

您可以使用列表推导,如下所示:

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])

输出:

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))

输出:

true
false

'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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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