Wanheda
Wanheda

Reputation: 25

Split array in multiple new arrays depending on the values

I need to split the array which plots the blue graph into multiple new arrays. Whenever the value of a number in the array goes from negative to positive or vice versa then it should save the number in a new array. So the result is multiple arrays with either just positive or just negative numbers. The code is NOT meant to merely combine the positive and the negative numbers in 2 diffenernt arrays. It is important that (for this example) I end up with 3 different arrays (first: only negatives, second: only positives, third: only negatives again).

I'm glad for any help I can get. Thank you!

Image of the blue line plot

1

In this case:

Difference (blue line) =  [-20.2 -19.7 -19.2 -18.9 -18.8 -18.8 -18.9 -18.9 -18.9 -18.9 -18.9 -18.9 -19.  -19.1 -19.1 -18.9 -18.5 -18.3 -18.9 -20.8 -24.1 -27.2 -28.1 -24.6  19.1  63.4 104.2 143.8 140.9 120.3  91.7  64.5  46.4  38.2  39.   47.8  63.3  82.9 103.5 122.1 136.4 147.1 155.3 162.5 169.7 177.  184.4 191.8 199.2 207.5 217.7 230.7 246.3 260.2 266.7 260.3 237.5 203.1 164.1 127.3  98.   75.3  56.7  39.9  22.7   5.5 -11.2 -26.5 -40.4 -54.9 -72.7 -96.5 -79.3 -67.2 -59.  -53.  -48.1 -44.  -40.7 -37.9 -35.6 -33.6 -31.9 -30.3 -28.7 -27.1 -25.7 -24.6 -23.8 -23.4 -23.  -22.7 -22.3 -21.9 -21.4 -20.8]

The 3 resulting arrays for this example should be:

Array1 = [-20.2 -19.7 -19.2 -18.9 -18.8 -18.8 -18.9 -18.9 -18.9 -18.9 -18.9 -18.9 -19.  -19.1 -19.1 -18.9 -18.5 -18.3 -18.9 -20.8 -24.1 -27.2 -28.1 -24.6]

Array2 = [19.1  63.4 104.2 143.8 140.9 120.3  91.7  64.5  46.4  38.2  39.   47.8  63.3  82.9 103.5 122.1 136.4 147.1 155.3 162.5 169.7 177.  184.4 191.8 199.2 207.5 217.7 230.7 246.3 260.2 266.7 260.3 237.5 203.1 164.1 127.3  98.   75.3  56.7  39.9  22.7   5.5]

Array3 = [-11.2 -26.5 -40.4 -54.9 -72.7 -96.5 -79.3 -67.2 -59.  -53.  -48.1 -44.  -40.7 -37.9 -35.6 -33.6 -31.9 -30.3 -28.7 -27.1 -25.7 -24.6 -23.8 -23.4 -23.  -22.7 -22.3 -21.9 -21.4 -20.8]

Upvotes: 2

Views: 734

Answers (2)

Patrick Artner
Patrick Artner

Reputation: 51653

Use a simple loop and put numbers int a new list of lists:

data = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 
         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
         9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 
        -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]

# new list of lists and start negative?
k = [[]]
neg = data[0] < 0

# loop all
for num in data:
    # if negativity of num identical add to last sublist in k
    if neg == (num<0):
        k[-1].append(num)
    # else reset negativity and append a new list
    else:
        neg = num<0
        k.append([num])

print(k)

Output:

[[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], 
 [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]]

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195438

If I understand you right, you want to group the array to positive/negative:

from itertools import groupby


l = [-20.2,-19.7,-19.2,-18.9,-18.8,-18.8,-18.9,-18.9,-18.9,-18.9,-18.9,-18.9,-19.,-19.1,-19.1,-18.9,-18.5,-18.3,-18.9,-20.8,-24.1,-27.2,-28.1,-24.6,19.1,63.4,104.2,143.8,140.9,120.3,91.7,64.5,46.4,38.2,39.,47.8,63.3,82.9,103.5,122.1,136.4,147.1,155.3,162.5,169.7,177.,184.4,191.8,199.2,207.5,217.7,230.7,246.3,260.2,266.7,260.3,237.5,203.1,164.1,127.3,98.,75.3,56.7,39.9,22.7,5.5,-11.2,-26.5,-40.4,-54.9,-72.7,-96.5,-79.3,-67.2,-59.,-53.,-48.1,-44.,-40.7,-37.9,-35.6,-33.6,-31.9,-30.3,-28.7,-27.1,-25.7,-24.6,-23.8,-23.4,-23.,-22.7,-22.3,-21.9,-21.4,-20.8]

out = []
for _, g in groupby(l, lambda x: x<0):
    out.append(list(g))

# print the lists:
for subl in out:
    print(subl)

Prints:

[-20.2, -19.7, -19.2, -18.9, -18.8, -18.8, -18.9, -18.9, -18.9, -18.9, -18.9, -18.9, -19.0, -19.1, -19.1, -18.9, -18.5, -18.3, -18.9, -20.8, -24.1, -27.2, -28.1, -24.6]
[19.1, 63.4, 104.2, 143.8, 140.9, 120.3, 91.7, 64.5, 46.4, 38.2, 39.0, 47.8, 63.3, 82.9, 103.5, 122.1, 136.4, 147.1, 155.3, 162.5, 169.7, 177.0, 184.4, 191.8, 199.2, 207.5, 217.7, 230.7, 246.3, 260.2, 266.7, 260.3, 237.5, 203.1, 164.1, 127.3, 98.0, 75.3, 56.7, 39.9, 22.7, 5.5]
[-11.2, -26.5, -40.4, -54.9, -72.7, -96.5, -79.3, -67.2, -59.0, -53.0, -48.1, -44.0, -40.7, -37.9, -35.6, -33.6, -31.9, -30.3, -28.7, -27.1, -25.7, -24.6, -23.8, -23.4, -23.0, -22.7, -22.3, -21.9, -21.4, -20.8]

Upvotes: 5

Related Questions