0

I have three arrays and one contains masked values based off some condition:

a = numpy.ma.MaskedArray(other_array, condition)

(Initially I used masked arrays because my condition is a variable and it made plotting a lot easier with other data sets to keep my arrays fixed lengths. Now I'm exporting my data to be analysed by other program not written by me, and it can't handle '--')

So my arrays have the form:

a = [1,--,3]
b = [4,5,6]
c = [7,8,9]

I want to iterate through a, identify any index of a that contains a masked value '--', and then delete that index from all arrays:

a = [1,3]
b = [4,6]
c = [7,9]

In reality, a b and c are very long, and the masked indices aren't regularly spaced.

Thanks!

rh1990
  • 880
  • 7
  • 17
  • 32
  • why 5 and 8 is removed from b and c? what particular problem you are facing to achieve your desired task? – Wasi Ahmad Dec 13 '16 at 10:58
  • In 'a' the masked value is in index 1, so I want to delete index 1 from all other arrays. I'll edit my post to make this clearer – rh1990 Dec 13 '16 at 11:00
  • what if there is a masked value in b or in c? clearly state all possible cases, otherwise you won't get a clear answer and please show your effort, i mean whatever you tried! – Wasi Ahmad Dec 13 '16 at 11:02
  • I say at the top that only one of the arrays, a, contains masked values – rh1990 Dec 13 '16 at 11:05

2 Answers2

1

If there are only 3 lists, you can use pop() function to delete the indexes from List B & C. Pass the Index to pop() where it is '--' in List A.

for i in range(len(a)):
    if numpy.ma.is_masked(a[i]):
        b.pop(i)
        c.pop(i)

It will delete that Index from the lists B & C, where '--' is present in List A.

Dinesh Suthar
  • 148
  • 2
  • 12
  • Hey thanks for the help. I tried this and everything is still in the list. Is it something to do with the masked array? – rh1990 Dec 13 '16 at 11:17
  • are you able to declare the list A with masked values in python? As, i am not even able to declare the list with masked values. – Dinesh Suthar Dec 13 '16 at 11:28
  • I updated the ***IF*** condition as per Masked values. Please check it. It is working fine now. – Dinesh Suthar Dec 13 '16 at 11:54
0

Try cloning the array and pop on it and replace the temp array like

a = [1,'--',3]
b = [4,5,6]
c = [7,8,9]
t=a[:]
for i in range(len(a)):
    try:
        value = int(a[i])
    except ValueError:
        t.pop(i)
        b.pop(i)
        c.pop(i)
a=t
print a
print b
print c

You can also use equal when you have the same symbol for masked value

a = [1,'--',3]
b = [4,5,6]
c = [7,8,9]
t=a[:]
for i in range(len(a)):
    if a[i]=='--':
        t.pop(i)
        b.pop(i)
        c.pop(i)
a=t
print a
print b
print c
jafarbtech
  • 6,842
  • 1
  • 36
  • 55