search包含数组python中的字符串

时间:2018-01-12 06:38:18

标签: python arrays

我有一个像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')

5 个答案:

答案 0 :(得分:6)

替换' ==' ' in'

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

答案 1 :(得分:2)

只需通过一个简单的循环遍历列表中的每个字符串,然后检查'hello'是否存在pythons membership in运算符:

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

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

哪个输出:

true
true
true

或者,如果您想立即检查lst if all('hello' in x for x in lst): print('true') 中的all()字符串:

lst

或者,如果您想同时检查if any('hello' in x for x in lst): print('true') any()字符串是否list()

true

两者都将输出:

list

注意:在问题中显示使用True作为变量名称不是一个好主意,因为它会影响内置函数Handlebars Intl。在这里返回一个布尔False { test: /\.tpl$/, include: [ path.resolve(__dirname, 'src/views'), ], use: { loader: 'handlebars-loader', options: { minimize: true, assumeObjects: true, knownHelpers: ['formatNumber'], knownHelpersOnly: false, helperDirs: [ path.resolve(__dirname, 'src/js/handlebar-helpers'), ], partialDirs: [ path.resolve(__dirname, 'src/views/partials') ], extensions: [ ".tpl" ] } } } 也没关系,不需要返回这些字符串形式。

答案 2 :(得分:1)

试试这个家伙:

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

答案 3 :(得分: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])

<强>输出:

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

答案 4 :(得分: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