0

I have the following array:

arr = [ nan nan nan 1.600e-01 1.000e+00 1.600e-01 9.999e-01 1.000e-04 1.600e-01 1.010e-01 nan 1.600e-01]

How I can obtain the following lists?

listA = [nan nan nan] #values in position 0, 1, 2
listB = [1.600e-01 1.000e+00 1.600e-01 9.999e-01 1.000e-04 1.600e-01 1.010e-01] #values in position 3 to 9
listC = [nan] #the values in position 10
listD = [1.600e-01] # the values in position 11

Thank you

4
  • listA has 2 items, but in comment you said there were 3 values. Is it a typo? Commented Oct 11, 2018 at 12:31
  • it's a trouble, here the correct output: Commented Oct 11, 2018 at 12:44
  • listA = [nan nan nan] #values in position 0, 1, 2 listB = [1.600e-01 1.000e+00 1.600e-01 9.999e-01 1.000e-04 1.600e-01 1.010e-01] #values in position 3 to 9 listC = [nan] #the values in position 10 listD = [1.600e-01] # the values in position 11 Commented Oct 11, 2018 at 12:44
  • listA = arr[:3] Commented Oct 11, 2018 at 12:51

1 Answer 1

3

use numpy split to split array based on a condition. the condition is defined whenever np.diff(np.isnan(arr))== True

np.split(arr, np.where(np.diff(np.isnan(arr))== True)[0]+1)

[array([nan, nan, nan]),
 array([1.600e-01, 1.000e+00, 1.600e-01, 9.999e-01, 1.000e-04, 1.600e-01,
        1.010e-01]),
 array([nan]),
 array([0.16])]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.