8

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')
2
  • 1
    try "hello" in "123hello123" Commented Jan 12, 2018 at 6:42
  • 2
    But you've replaced the whole loop with in. That's not what Rakesh told you to do. Commented Jan 12, 2018 at 6:59

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Why not for l in lst: if 'hello' in l: The Variable filename seems a little redundant....
@RoadRunner this example work perfetly, i use if any('hello' in x for x in lst): but mayby you know why this is false search_word = Taxi Stockholm Kostnad för resa 545 kr.pdf, and list contains [email protected]_Taxi Stockholm Kostnad för resa 545 kr.pdf why it output false?
6

Replace '==' with 'in'

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

3 Comments

thks for answered, but i try you example and it not work, please see https://repl.it/repls/MintcreamDarkslategreyAustrianpinscher
Sorry I am unable to open the link you provided. Can you post what you tried in the comments?
if not any(file['title'] in filename for file in list_all_files_in_google_drive): print('file not exist') this is how to use 'in'
1

try this one dude :

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

2 Comments

filename = '555' list = ['123hello123', 'aasdasdasd123hello123', '123123hello'] if (filename.find(file) for file in list): print('true') else: print('false') it always output true
then change filename.find(file) to filename in file and try
1

You can use list comprehensions as shown below:

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

Another solution is to have a own function as follows:

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

Comments

0

'in' operator is good solution , You can also try regex :

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

output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.