In Python, I have a list of lists like the following:
[[1,2,3, 'L'], ['L'], [1]]
and I want to compute for each sublist the average over all the numerical elements. Values ‘L’ should thus be excluded. The result for the above example should be:
[2, [], 1]
Is there any quick way of doing this in one line?
Thanks.
Here’s a highly unreadable one-liner, assuming you’ve already imported
numpyandnumbers. Lists with no numeric elements show up asnanin the resulting list.