Search code examples
pythonlistnanidl-programming-language

python: eliminate positions of nan in multiple lists


I am coming from IDL and trying to find and eliminate the NaNs in two lists of data. lets say there is a NaN in position 5 for list A but not list B. I need position 5 to be removed in both lists. Like so...

A = [1, NaN, 3, 4, NaN, 6, 7, 8, 9]

B = [1', 2', 3', NaN, 5', 6', 7', 8', NaN]

A_new = [1 , 3 , 6 , 7 , 8 ]

B_new = [1', 3', 6', 7', 8']

Here is the IDL code that works fine. I just need it translated to python and I am stumped.

;Removes the NANs

loc = where((Finite(pdcsapflux) EQ 1)and(Finite(time) EQ 1))

flux_nonan = pdcsapflux(loc)

time_nonan = time(loc)

Thanks!!


Solution

  • I dont know if python has NaN, just assume that is None.

    ta, tb = [], []
    for i in range(min(len(a), len(b))):
        if a[i] is None or b[i] is None:
            continue
        ta.append(a[i])
        tb.append(b[i])
    

    ta, tb is your output of a,b at last you should append the rest item of the longer list.