dimanche 3 mai 2020

for loop causes to print unwanted elements, how to write the if statement?

in my re_list i have 4 condition, now when i iterate through the row, it gives me the output if the required info exists, and if not it gives me empty list. if re.findall doesnt find any match: i want only 1 empty list How can i avoid from getting extra 3 emppty list? how should the if condition be here?

check for the output adn the expected downbelow

this is the whole code

fil = data['transcription']
re_list = [
    '\d*\-year-old',
    '\d*\ year old',
    '\d*\ years old',
    '\d*\ y/o'
]


matches = []

for i in fil:

    try:
#         i.replace('-',' ')

        for r in re_list:
            matches.append(re.findall(r,i))
        else:
            pass
#             matches+=(' ')
    except:
        pass
#         matches+=[]

    print((matches))

something like this doesn't work, and yeah i know why, how i can write the if statement with the re.findall condition.

   for r in re_list:
       if r in i:
           matches.append(re.findall(r,i))

output:

[['23-year-old'], [], [], []]
[['23-year-old'], [], [], [], [], [], [], []]
[['23-year-old'], [], [], [], [], [], [], [], [], [], ['42 years old'], []]
[['23-year-old'], [], [], [], [], [], [], [], [], [], ['42 years old'], [], [], [], [], []]
[['23-year-old'], [], [], [], [], [], [], [], [], [], ['42 years old'], [], [], [], [], [], [], [], [], []]
[['23-year-old'], [], [], [], [], [], [], [], [], [], ['42 years old'], [], [], [], [], [], [], [], [], [], ['30-year-old'], [], [], []]

expected:

[['23-year-old']]
[['23-year-old'], []]
[['23-year-old'], [], ['42 years old']]
[['23-year-old'], [], ['42 years old'], []]
[['23-year-old'], [], ['42 years old'], [], []]
[['23-year-old'], [], ['42 years old'], [], [], ['30-year-old']]

Aucun commentaire:

Enregistrer un commentaire