python - To much logic inside list comprehension? -


i have list comprehension in code looks :

datapointlist = [      map(str,          [ elem elem in dataframe[column]             if not pd.isnull(elem) , '=' in elem         ]      ) column in list(dataframe) ] 

i wondering, there general rule of thumb when break list comprehension? can have logic inside list comprehension?

you don't want mix map , list comprehensions, when map isn't needed @ all:

>>> list(map(str, [x x in [1, 2, 3]])) ['1', '2', '3'] >>> [str(x) x in [1, 2, 3]] ['1', '2', '3'] 

it means can apply str[elem] directly in list comprehension:

datapointlist = [     [ str(elem) elem in dataframe[column]        if not pd.isnull(elem) , '=' in elem     ] column in list(dataframe) ] 

then, dataframe iterable. there's no need convert list:

>>> [x x in list(pd.dataframe(d))] ['one', 'two'] >>> [x x in pd.dataframe(d)] ['one', 'two'] 

your code becomes :

datapointlist = [     [ str(elem) elem in dataframe[column]        if not pd.isnull(elem) , '=' in elem     ] column in dataframe ] 

note since want nested list, cannot use double list comprehension:

>>> [(a,b) in x b in y] [(1, 3), (1, 4), (2, 3), (2, 4)] >>> [[(a,b) b in y] in x] [[(1, 3), (1, 4)], [(2, 3), (2, 4)]] 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -