0

I have a masked array Xpos:

masked_array(
  data=[[--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [--, --, --, --, --, --, --, --, --, --],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0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0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
    [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]],
 mask=[[ True,  True,  True, ...,  True,  True,  True],
    [ True,  True,  True, ...,  True,  True,  True],
    [ True,  True,  True, ...,  True,  True,  True],
    ...,
    [False, False, False, ..., False, False, False],
    [False, False, False, ..., False, False, False],
    [False, False, False, ..., False, False, False]],
fill_value=9.96921e+36,
dtype=float32)

And when I ask Xpos[3000,:] this is the output:

[-3.802131175994873 -3.8107831478118896 -3.7940280437469482
 -3.838264226913452 -3.8027470111846924 -3.766021251678467
 -3.8610005378723145 -3.8038835525512695 -3.7840664386749268
 -3.7463834285736084]

So a bunch of masked values, a bunch of numbers and a bunch of zeros. I want a new array with no masked values and no zeros, keeping the same structure of the array. The code I am using is this:

Xpos1=[]
for i in range (0,len(Xpos)):
    if Xpos[i,0]!=0: 
       Xpos1=np.append(Xpos1,Xpos[i,:] ) 

BUT this gives me one long array with values, and does not retain the original structure like Xpos: [[a,b,c,],[a,b,c,],[a,b,c,]...] with abc numbers. So for output, I would like something likeXpos1=[[a,b,c,],[a,b,c,],[a,b,c,]...]with abc numbers that are not all zero.

How do I improve my coding to keep it a list of arrays (forgive me if I'm using the wrong terminology)?

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
Jellyse
  • 839
  • 7
  • 22
  • 1
    So with `I want a new array with no masked values and no zeros, keeping the same structure of the array` in mind, what is the expected output? – depperm Apr 27 '18 at 19:27
  • What value is used as mask? "-" is invalid syntax. – What Apr 27 '18 at 19:29
  • @What it's empty values that are masked. – Jellyse Apr 27 '18 at 19:37
  • @Jellyse then use my answer and instead of mask = "?" it should be mask = None – What Apr 27 '18 at 19:38
  • Why are there no "," for the values you posted? – What Apr 27 '18 at 19:56
  • If I use the following structure `Xpos=[[None,None,None,None,None,None,None,None,None,None], [None,None,None,None,None,None,None,None,None,None], [-3.80, -3.81, -3.79, -3.83, -3.80, -3.76, -3.86, -3.80, -3.78, -3.74], [-3.80, -3.81, -3.79, -3.83, -3.80, -3.76, -3.86, -3.80, -3.78, -3.74], [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0], [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0]]` instead of the one you posted the result is what I would expect. – What Apr 27 '18 at 19:58

3 Answers3

1

As list literal it could look like this:

lst1 = [y for y in [x for x in Xpos] if y[0] != "?" if y[0] != 0]
print(lst1)

Giving you:

[[1, 2, 3], [2, 3, 4]]

Or as written out code:

#!/usr/bin/env python

Xpos = [["?", "?", "?"],
        ["?", "?", "?"],
        [1, 2, 3],
        [2, 3, 4],
        [0, 0, 0],
        [0, 0, 0]]

mask = "?" # may replace mask with None.
XPos2 = []
for subarray in Xpos:
    for element in subarray:
        if element == mask or element == 0:
            break
        else:    
            XPos2.append(subarray)
            break

Will give you:

[[1, 2, 3],[2, 3, 4]]
What
  • 304
  • 1
  • 12
  • My original array is much bigger, and for some reason, every seventh number is deleted when I use this code. So that would be '[[a b c d e -- g],[a b c d e -- g],[a b c d e -- g],...]'. – Jellyse Apr 27 '18 at 19:47
  • @Jellyse could you update your question with bigger data sample? – What Apr 27 '18 at 19:50
1

I'm not an expert on masked arrays but this works:

# create example
>>> a = np.arange(30).reshape(10, 3)
>>> a[[0,7,8]] = 0
>>> a = np.ma.MaskedArray(a, np.isin(a // 3, (2,3)))
>>> 
>>> a
masked_array(
  data=[[0, 0, 0],
        [3, 4, 5],
        [--, --, --],
        [--, --, --],
        [12, 13, 14],
        [15, 16, 17],
        [18, 19, 20],
        [0, 0, 0],
        [0, 0, 0],
        [27, 28, 29]],
  mask=[[False, False, False],
        [False, False, False],
        [ True,  True,  True],
        [ True,  True,  True],
        [False, False, False],
        [False, False, False],
        [False, False, False],
        [False, False, False],
        [False, False, False],
        [False, False, False]],
  fill_value=999999)
>>> 
# cut all rows that have at least one masked or all zero entries
>>> compressed = a.data[~np.any(a.mask, axis=1) & np.any(a.data!=0, axis=1)]
>>> compressed
array([[ 3,  4,  5],
       [12, 13, 14],
       [15, 16, 17],
       [18, 19, 20],
       [27, 28, 29]])
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0

Try something like this:

Xpos=[["","",""],
  ["","",""],
  [1,2,3],
  [2,3,4],
  [0,0,0],
  [0,0,0]]

Xpos1 = []
for inner_list in Xpos:
    if 0 in inner_list or "" in inner_list:
        pass
    else:
        Xpos1.append(inner_list)

print Xpos1

basically you can check for the values that you don't want and then only append the lists that you want to the final list.