python - How to delete in-place items in arrays in a list of arrays -


i used following line:

list_split_test_array = np.split(test_array, np.where(test_array == 0.)[0]) 

to split array @ each 0., value arbitrarily added place holder.
gives me list of arrays 0th index of each array (except first array) 0. how delete these 0's in-place, list of arrays without placeholder 0's? in-place because i'd rather not iterate through, adding each array deleted 0 new array list.

you can use preprocess remove zeros, here example:

import numpy np = np.random.randint(0, 5, 100) idx = np.where(a == 0)[0]  np.split(a, idx) 

output:

[array([4, 3]),  array([0, 3, 1, 1]),  array([0, 4]),  array([0]),  array([0, 1, 2, 4]),  array([0, 4, 2, 2, 3, 1]),  array([0, 2, 4, 3]),  array([0, 2, 2, 4]),  array([0, 3, 2, 1, 2, 4, 1, 2]),  array([0]),  array([0]),  array([0, 1, 3]),  array([0]),  array([0, 1, 3, 2, 4]),  array([0, 3, 3]),  array([0, 3]),  array([0, 3, 4, 1, 2, 3, 3, 4, 3, 4, 2, 1]),  array([0]),  array([0, 3, 2]),  array([0]),  array([0, 4, 1, 4, 2, 3, 1]),  array([0, 4]),  array([0]),  array([0]),  array([0, 3, 4, 2, 3]),  array([0, 4, 4, 3, 3, 3, 1]),  array([0, 2, 4, 1]),  array([0, 4, 2, 2]),  array([0])] 

remove zeros in a, , calculate new index:

idx2 = idx - np.arange(len(idx)) b = np.delete(a, idx) np.split(b, np.unique(idx2)) 

here output:

[array([4, 3]),  array([3, 1, 1]),  array([4]),  array([1, 2, 4]),  array([4, 2, 2, 3, 1]),  array([2, 4, 3]),  array([2, 2, 4]),  array([3, 2, 1, 2, 4, 1, 2]),  array([1, 3]),  array([1, 3, 2, 4]),  array([3, 3]),  array([3]),  array([3, 4, 1, 2, 3, 3, 4, 3, 4, 2, 1]),  array([3, 2]),  array([4, 1, 4, 2, 3, 1]),  array([4]),  array([3, 4, 2, 3]),  array([4, 4, 3, 3, 3, 1]),  array([2, 4, 1]),  array([4, 2, 2]),  array([], dtype=float64)] 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -