Home > OS >  while accessing findall retuned listt through an error
while accessing findall retuned listt through an error

Time:10-30

Hi I'm trying to search for a pattern using find all and it will return list if there is a match. and I'm trying to access that list which throughs an error like (IndexError: list index out of range) and my snippet i wrote is like below.

return_from_findall = re.findall(regex, input)

    if return_from_findall:
        ##trying to print the list element when list returned from finall is true##
        print(return_from_finall[0])
        ## also trying to compare the list with another string like
        if return_from_finall[0] == somestring:
            print(match found)

Both are not working

can some one help to solve this problem

My program:

import re

output = """
Another option is to use the name randomizer.
to randomize all the names on your list. 
In this case, you arent using it as a 
random name picker, but as a true name 
randomizer. For example, 
"""

match = 0

search_item = "Another option is to use the name randomizer"

expected_list = [
    'Another option is to use the name randomizer.',
    'to randomize all the names on your list.',
    'In this case, you arent using it as a',
    'random name picker, but as a true name',
    'randomizer. For example,']
    
for line in output.splitlines():
    line = line.strip()
    print(" ################### ")

    return_from_findall = re.findall(search_item, line)

    print("searched line - ")
    print(return_from_findall)

    print("expected line - ")
    print(expected_list[match])

    if return_from_findall:
        if return_from_findall[0] == expected_list[match]:
            print("found match")
        

CodePudding user response:

If you are looking to see how many matches you have, you could use the following:

import re

output = """
Another option is to use the name randomizer.
to randomize all the names on your list. 
In this case, you arent using it as a 
random name picker, but as a true name 
randomizer. For example, 
"""

match = 0
search_item = "Another option is to use the name randomizer"
    
for line in output.splitlines():
    line = line.strip()
    
    return_from_findall = re.findall(search_item, line)
    if return_from_findall:
        match  = 1

print(f"Matches: {match}")

OUTPUT:

Matches: 1

CodePudding user response:

If you are looking to compare your lines in expected_list to see if they appear in the output string, then you could do something like this:

output = """
Another option is to use the name randomizer.
to randomize all the names on your list. 
In this case, you arent using it as a 
random name picker, but as a true name 
randomizer. For example, 
"""

expected_list = [
    'Another option is to use the name randomizer.',
    'to randomize all the names on your list.',
    'In this case, you arent using it as a',
    'random name picker, but as a true name',
    'randomizer. For example,']

for line in expected_list:
    if line in output:
        print("Found: ", line)
    else:
        print("Not found: ", line)

OUTPUT:

Found:  Another option is to use the name randomizer.
Found:  to randomize all the names on your list.
Found:  In this case, you arent using it as a
Found:  random name picker, but as a true name
Found:  randomizer. For example,
  • Related