mardi 9 mars 2021

pythonic way to simplify this bunch of if statements?

Is there a way to simplify this bunch of if statements with a list?

my_lnk = 'somesite/files/file.mp4'

if '.mp4' in my_lnk:
    f_ext = '.mp4'

if '.webm' in my_lnk:
    f_ext = '.webm'

# and so on for other file types

maybe something like this but i need to assign '.mp4' to f_ext:

f_exts = ['.mp4', '.webm', '.png']

my_lnk = 'somesite/files/file.mp4'

if my_lnk in f_exts:
    print('found it')

Aucun commentaire:

Enregistrer un commentaire