我有这样一个字符串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" | trueUPD:
我检查了运营商,它不输出true
filename = 'hello' list = ['123hello123', 'aasdasdasd123hello123', '123123hello'] if filename in list: print('true')I have a string like this search = 'hello' and I need check all array to see if any contains hello. For example:
"hello" == "123hello123" | true "hello" == "aasdasdasd123hello123" | true "hello" == "123123hello" | trueI need something like search for my code
list_all_files_in_google_drive - it my all file name array
filename - name file, which need to check if exist in google drive or not
if not any(file['title'] == filename for file in list_all_files_in_google_drive): print('file not exist')my code doesn't work because it works like this:
"hello" == "123hello123" | false "hello" == "aasdasdasd123hello123" | false "hello" == "123123hello" | false "hello" == "hello" | trueand I need it to work like this:
"hello" == "123hello123" | true "hello" == "aasdasdasd123hello123" | true "hello" == "123123hello" | true "hello" == "hello" | trueUPD:
I checked operator in and it does not output true
filename = 'hello' list = ['123hello123', 'aasdasdasd123hello123', '123123hello'] if filename in list: print('true')最满意答案
只需用一个简单的循环遍历列表中的每个字符串,然后检查运算符中的pythons成员是否存在'hello' :
lst = ['123hello123', 'aasdasdasd123hello123', '123123hello'] for x in lst: if 'hello' in x: print('true')哪些输出:
true true true或者如果你想一次查看all()字符串:
if all('hello' in x for x in lst): print('true')或者,如果你想一次检查是否有any()的字符串:
if any('hello' in x for x in lst): print('true')两者都会输出:
true注意:在你的问题中使用list作为变量名不是一个好主意,因为它会影响内置函数list() 。 在这里返回一个布尔值True或False是好的,不需要返回这些字符串形式。
Just go through each string in the list with a simple loop, and check if 'hello' exists with the pythons membership in operator:
lst = ['123hello123', 'aasdasdasd123hello123', '123123hello'] for x in lst: if 'hello' in x: print('true')Which outputs:
true true trueOr if you want to check all() the strings in lst at once:
if all('hello' in x for x in lst): print('true')Or if you want to check if any() of the strings in lst at once:
if any('hello' in x for x in lst): print('true')Both of which will output:
trueNote: Using list as a variable name as shown in your question is not a good idea here, as it shadows the builtin function list(). Also returning a boolean True or False here is fine, not need to return a string form of these.
更多推荐
发布评论