[Python] Return elements from a list that match a pattern

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I have a list of words as below:

    STR = ['aa', 'dffg', 'aa2', 'AAA3']


    I want to get a list of elements from the above list that match a string:

    to_match = 'aa'


    I tried the code below:

    import re
    [re.findall(to_match, iWord) for iWord in STR]
    # [['aa'], [], ['aa'], []]


    However, I wanted to get a list like ['aa', 'aa2', 'AAA3'].

    Continue reading...

Compartilhe esta Página