Python - Split Array into Multiple Arrays

Python - Split array into multiple arrays

Something like that should work:

import itertools


mylist = [...]
[list(v) for k,v in itertools.groupby(mylist, key=lambda x: x[:3])]

If input list isn't sorted, than use something like that:

import itertools


mylist = [...]
keyfunc = lambda x:x[:3]
mylist = sorted(mylist, key=keyfunc)
[list(v) for k,v in itertools.groupby(mylist, key=keyfunc)]

How to split an array into multiple arrays based on a specific value in python

You can achieve that relatively easy with iterator:

import numpy as np

def split_by_min(arr):
equals = np.isclose(arr, np.abs(arr).min())

l = []
is_ready = False
for i in range(len(arr)):
if equals[i] and i < len(arr)-1 and equals[i+1]:
is_ready = True
elif not equals[i] and is_ready:
yield l
l = []
is_ready = False

if not is_ready:
l.append(arr[i])

yield l




arr = np.array([-0.005,-0.004,0.00002,-0.003,0.004,0.00002,0.00002,0.00002,
0.00002,0.005,0.003,0.00002,0.0006,0.008,0.00002,0.00002,
0.00002,-0.003,0.006,0.007,0.00002,0.00002,0.00002])

for l in split_by_min(arr):
print(l)

Output:

[-0.005, -0.004, 2e-05, -0.003, 0.004]
[0.005, 0.003, 2e-05, 0.0006, 0.008]
[-0.003, 0.006, 0.007]

Split an array into multiple arrays by condition

Here is one approach:

import numpy as np
np.random.seed(0)

# get an increasing array
arr = np.random.randint(0, 400, size=50).cumsum()

bracket_size = 2000
brackets = np.arange(0, arr.max() + bracket_size, bracket_size)
# array([ 0, 2000, 4000, 6000, 8000, 10000])

result = np.split(arr, np.unique(np.searchsorted(brackets, arr), return_index=True)[1])
# [array([], dtype=int64),
# array([ 172, 219, 336, 528, 851, 1102, 1297, 1656, 1665, 1876]),
# array([2153, 2395, 2687, 2774, 2844, 2932, 3328, 3642, 3835, 3874, 3961]),
# array([4135, 4223, 4560, 4725, 4750, 5083, 5155, 5420, 5535, 5778, 5975]),
# array([6310, 6648, 6747, 6924, 7167, 7452, 7599, 7746]),
# array([8144, 8432, 8697, 8882, 9009, 9041, 9072, 9274, 9518, 9669])]

You can first compute an array of "brackets" that specifies where each element of your array should go. Then, you can assign each array element to the correct bracket using np.searchsorted. Since split expects a list of indices where the arryay should be split, one can use np.unique to find the first index where a split for each bracket should occur.

Avoid for-loop to split array into multiple arrays by index values using numpy

Does this suffice?

Solution 1 (Note: for loop is not over the entire index array)

import numpy as np

value_array = np.array([56, 10, 65, 37, 29, 14, 97, 46])
index_array = np.array([ 0, 0, 1, 0, 3, 0, 1, 1])

max_idx = np.max(index_array)
split_array = []

for idx in range(max_idx + 1):
split_array.append([])
split_array[-1].extend(list(value_array[np.where(index_array == idx)]))
print(split_array)
[[56, 10, 37, 14], [65, 97, 46], [], [29]]

Solution 2

import numpy as np

value_array = np.array([56, 10, 65, 37, 29, 14, 97, 46])
index_array = np.array([ 0, 0, 1, 0, 3, 0, 1, 1])

value_array = value_array[index_array.argsort()]
split_idxs = np.squeeze(np.argwhere(np.diff(np.sort(index_array)) != 0) + 1)
print(np.array_split(value_array, split_idxs))
[array([56, 10, 37, 14]), array([65, 97, 46]), array([29])]

Split array in multiple new arrays depending on the values

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]

How to split an array into chunks of a given length in python?

Why don't you try out a list comprehension?

Example:

[ids[i:i+2] for i in range(0,len(ids),2)]

Output:

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



Related Topics



Leave a reply



Submit