简体   繁体   中英

Any better way to write list comprehension?

I'm trying to remove whitespace and 'the' from an artist name. But my list comprehension seems not readable.

Is there any better way to write this list comprehension or I just use classic for loop?

 new_list= [artist.lower().replace(' ','') if artist[0:3] != 'the' else artist.lower().replace(' ','')[3:] for artist in artist_list]

You were right in thinking that if the expression in a list comprehension gets too big, it would be better to use a straight for-loop.

That said, the expression can be simplified by applying str.replace() to remove 'the' without a test ( replace is silent if there are no replacements):

>>> s = 'The Artist Formerly Known as Prince'
>>> s.lower().replace('the ', '').replace(' ', '')
'artistformerlyknownasprince'

Alternatively, the whole process might be simpler and more flexible with a regular expression:

>>> re.sub(r'(^the)|\s', '', s.lower())
'artistformerlyknownasprince'

It's still not illegal to pull your tricky bits out into a function:

def cleanup(artist):
    artist = artist.lower().replace(' ', '')
    if artist.startswith('the'):
        artist = artist[3:]
    return artist


new_list = [cleanup(artist) for artist in artist_list]

This has the big advantages (in my opinion) that you can test and debug the parts separately and that it's very clear what each step is doing.

BTW, you'll want to test and debug that cleanup portion right off the bat, as that approach will do weird things to They Might Be Giants.

You can combine a comprehension with a regex:

import re
new_list = [re.sub("^the ", "", artist.lower()).replace(" ", "") for artist in artist_list]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM