Search code examples
pythonironpython

Get certain items based on their formatting


I have a list of values, some are numeric only, others made up of words, others a mix of the two. I would like to select only those items composed by the combination number, single letter, number.

let me explain, this is my list of values

l = ['980X2350', 'DO_UN_HPL_Glas_Links', 'DO_UN_HPL_Glas_Rechts',
     '930x2115', 'DO_UN_HPL_Links', 'DO_UN_HPL_Rechts', '830X2115',
     'Deuropening', 'BF_32_Tourniquets_dubbeledeur_Aluminium']

I'd like to just get back:

['980X2350', '930x2115', '830X2115'] 

Solution

  • Assuming a list of strings as input, you can use a regex and a list comprehension:

    l = ['980X2350', 'DO_UN_HPL_Glas_Links', 'DO_UN_HPL_Glas_Rechts',
         '930x2115', 'DO_UN_HPL_Links', 'DO_UN_HPL_Rechts', '830X2115',
         'Deuropening', 'BF_32_Tourniquets_dubbeledeur_Aluminium']
    import re
    regex = re.compile('\d+x\d+', flags=re.I)
    
    out = [s for s in l if regex.match(s.strip())]
    

    output:

    ['980X2350', '930x2115', '830X2115']