python--recursive list comprehension to apply lstrip on a list -


i have 2 lists follows

f = ['sum_','count_','per_'] d = ['fav_genre','sum_fav_event','count_fav_type','per_fav_movie'] 

so want apply lstrip of each strings in f on items of list d,,so can get

d = ['fav_genre','fav_event','fav_type','fav_movie'] 

and want using list comprehension. know can in other ways also, using re.sub, applying replace on each time on list items of d

 #example  d = [re.sub(r'.*fav', 'fav', x) x in d] #####gives want  ## if fav (which in case matching pattern) not there in d solution won't work  ## d = ['fav_genre','sum_any_event','count_some_type','per_all_movie']  #re.sub can't applied on d(as before) no matching char 'fav' found  

so list compression choose do..

so far have tried ..

d_one = [x.lstrip('count_') x in d]   ###only count_ stripped # o/p- d-one = ['fav_genre', 'sum_fav_event', 'fav_type', 'per_fav_movie'] # c_n apply lstrip of each string f on items of d ## why not apply items lstrip in 1 go ### tried d_new = [x.lstrip(y) y in f x in d] ###['fav_genre', 'fav_event', 'count_fav_type', 'per_fav_movie', 'fav_genre', 'sum_fav_event', 'fav_type', 'per_fav_movie', 'fav_genre', 'sum_fav_event', 'count_fav_type', 'fav_movie'] 

so gave me results each iteration of lstrip applied

please suggest how can apply lstrip in 1 go in list comprehension (recursively). in advance.

try this:

>>> f = ['sum_','count_','per_'] >>> d = ['fav_genre','sum_fav_event','count_fav_type','per_fav_movie'] >>> [s[len(([p p in f if s.startswith(p)]+[""])[0]):] s in d] ['fav_genre', 'fav_event', 'fav_type', 'fav_movie'] 

i believe handles of cases intended.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -