0

I have a list whose name is Ave. Ave[0] is shown below:

[array([ nan]), array([ 0.03030303]), array([ 0.025]), array([ 0.03546099]), array([ 0.02877698]), array([ 0.05343511]), array([ nan]), array([ nan])] 

I need to remove the nan value from each list in the Ave. my code works slowly:

for j in range(len(Ave)): c=0 while c<8: for i in range(len(Ave[j])): if np.isnan(Ave[j][i])==True: del Ave[j][i] break c=c+1 

It would be appreciated if anybody gives me a faster code.

2
  • are you using numpy? if yes, please update your tag. Commented Apr 13, 2018 at 0:54
  • 1
    What exactly are you trying to do? Is the list always of length 8? Do the numpy arrays always only have 1 element? Some more elaboration can help us help you. Commented Apr 13, 2018 at 1:25

3 Answers 3

1

If I understand your problem correctly, this is one way.

Just note that holding a list of arrays is inefficient. It is advisable, where possible, to hold your data in a single numpy array.

from numpy import array, isnan, nan Ave = [array([ nan]), array([ 0.03030303]), array([ 0.025]), array([ 0.03546099]), array([ 0.02877698]), array([ 0.05343511]), array([ nan]), array([ nan])] A = array(Ave) res = list(map(array, A[~isnan(A)])) # [array(0.03030303), # array(0.025), # array(0.03546099), # array(0.02877698), # array(0.05343511)] 
Sign up to request clarification or add additional context in comments.

Comments

0

You could use list comprehensions:

ave = [ [ n for n in row if not np.isnan(n)] for row in ave ] 

Note that your sample data is a bit ambiguous as it is not clear if, by "Ave[0] is shown below", you mean to imply that the list has three dimensions or just two. It seams unlikely that you would want a 3D list with the second dimension containing lists of one-element arrays. The result of the NaN clean up would produce rows with a list of arrays of 0 or 1 element.

If that is indeed what you're working with, then there would need to be one more nesting of list comprehension in the solution, at which point I would suggest breaking it into steps with intermediate results.

for example:

clearNaN = lambda numberArray : [ n for n in numberArray if not np.isnan(n) ] clearList = lambda arrayList : [ clearNaN(arr) for arr in arrayList ] Ave = [ clearList(row) for row in Ave ] 

Comments

0

You can also use itertools.filterfalse:

Ave # [array([nan]), array([0.03030303]), array([0.025]), array([0.03546099]), array([0.02877698]), array([0.05343511]), array([nan]), array([nan])] list(itertools.filterfalse(np.isnan, Ave)) # [array([0.03030303]), array([0.025]), array([0.03546099]), array([0.02877698]), array([0.05343511])] 

Or if Ave is nested deeper:

AveAveAve = np.random.randint(0, 3, (5, 5, 1)) AveAveAve = np.random.random((5, 5, 1)) * AveAveAve / AveAveAve AveAveAve = list(map(list, AveAveAve)) AveAveAve # [[array([0.25051587]), array([nan]), array([0.93294353]), array([0.45979654]), array([0.46274847])], # [array([nan]), array([0.36631801]), array([0.18432189]), array([0.10408241]), array([nan])], # [array([0.73367427]), array([0.93523268]), array([0.33142847]), array([nan]), array([0.84569255])], # [array([0.95316363]), array([0.67298091]), array([0.06368582]), array([0.22668049]), array([0.14361035])], # [array([0.66480064]), array([0.91260134]), array([0.10735208]), array([0.55708037]), array([0.6402967])]] list(map(list, map(itertools.filterfalse, itertools.repeat(np.isnan), AveAveAve))) # [[array([0.25051587]), array([0.93294353]), array([0.45979654]), array([0.46274847])], # [array([0.36631801]), array([0.18432189]), array([0.10408241])], # [array([0.73367427]), array([0.93523268]), array([0.33142847]), array([0.84569255])], # [array([0.95316363]), array([0.67298091]), array([0.06368582]), array([0.22668049]), array([0.14361035])], # [array([0.66480064]), array([0.91260134]), array([0.10735208]), array([0.55708037]), array([0.6402967])]] 

Comments