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
Post a Comment