搜索包含数组python中的字符串(search contains string in array python)

编程入门 行业动态 更新时间:2024-10-21 23:15:03
搜索包含数组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 - 名称文件,需要检查是否存在谷歌驱动器或不

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:

我检查了运营商,它不输出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" | true

I 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" | true

and I need it to work like this:

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

UPD:

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 true

Or 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:

true

Note: 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.

更多推荐

本文发布于:2023-07-06 07:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   python   array   search

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!