samedi 8 septembre 2018

"IF and ELIF" work as "FOR of IF" in Python?

I have a code like:

if 'x' in longstr:
   longstr.replace('x', '')
elif 'y' in longstr:
   longstr.replace('y', '')
elif 'z' in longstr:
   longstr.replace('z', '')
else:
   longstr.replace('xyz', '')
...
...

If I wanted to use it instead:

for str in ['x','y','z']:
   if str in longstr:
      longstr.replace(str, '')
      break
...
...     

How should I set "ELSE" final/general [else: longstr.replace('xyz', '')] and continue with next codes of function?
And above all, which one is more performing?

Aucun commentaire:

Enregistrer un commentaire