dimanche 29 juillet 2018

elif statement without else in list comprehension

I'm doing some string parsing and want to return 1 if the character is a letter, 2 if the character is a number, and pass if the character is anything else.

Normally I would just use a for loop and append to it like this

string = 'hello123...'
values = []
for char in string:
    if char.isalpha():
        values.append(1)
    elif char.isdigit():
        values.append(2)

which returns

[1, 1, 1, 1, 1, 2, 2, 2]

as expected. According to https://stackoverflow.com/a/30245465/10029403 using list comprehensions can be much faster. So I tried:

values = [1 if char.isalpha else 2 if char.isdigit for char in string]

However, this is giving me a syntax error as an 'else' is expected.

File "C:/Users/test3.py", line 12
values = [1 if char.isalpha else 2 if char.isdigit for char in string]
                                                     ^
SyntaxError: invalid syntax

I want it to add nothing if the character is anything but alphanumeric. What is wrong with my code?

I will have to execute this function possibly billions of times so if there is a better way to do this, any increase in efficiency is welcome.

Aucun commentaire:

Enregistrer un commentaire