0

Say you have the following list:

import numpy as np

mylist=[8.4468975,
        8.52036,
        9.3605785,
        np.ma.core.MaskedConstant(),
        np.ma.core.MaskedConstant()]  

 # [8.4468975, 8.52036, 9.3605785, masked, masked]         

and you want to assign 0 to all values that are masked, and 1 to all the others. How can you properly do it?

I've tried this, but the result is an empty list:

mylist=[1 for i,v in enumerate(mylist) if type(mylist[i]) is float]

The result should be numerical (no True/False):

mylist=[1,1,1,0,0]

EDIT

The masked values that are present in my original list are of the type numpy.ma.core.MaskedConstant.

0

5 Answers 5

2

Like this:

mylist=[8.4468975,
        8.52036,
        9.3605785,
        masked,
        masked]

mylist = [0 if type(x) is numpy.ma.core.MaskedConstant else 1 for x in mylist]

Also note that your list comprehension mylist=[1 for i,v in enumerate(mylist) if type(mylist[i]) is float] does not quite do what you describe; at least not in the general case. You are assigning 1 to anything that is not float which is not the same as to anything that is 'masked'.

Also note that when looping like [1 for i,v in enumerate(mylist) if type(mylist[i]) is float] you are (falsely though, see further down) not using the v so you could rewrite as [1 for i,_ in enumerate(mylist) if type(mylist[i]) is float]. Notice the underscore. It is used to imply that you will be dumping the second item of the tuple returned by enumerate().

Finally, i said falsely further up because in your list comprehension mylist[i] can be substituted by v. So properly done it would be written like: [1 for v in mylist if type(v) is float]. But as I said, this is slightly different from what you seem to want.

Sign up to request clarification or add additional context in comments.

2 Comments

I had actually forgot an important thing regarding the type of the masked instances. They are numpy.ma.core.MaskedConstant, and not strings. So your approach does not work the way it is now.
@CF84 Now your workaround makes more sense. I will read a bit on those and edit my answer
2

You can use int() to cast your booleans to integers:

[int(isinstance(x,float)) for x in mylist]

Note this works because numpy.ma.core.MaskedConstant() is not a float.

Comments

1

You can try this:

mylist = [1 if isinstance(v, float) else 0 for i,v in enumerate(mylist)]

Alternatively, you could do:

mylist = [0 if isinstance(v, np.ma.core.MaskedConstant) else 1 for i,v in enumerate(mylist)]

so that you only set to 0 a specific mask type.

The relevant function to check for the type of a variable is: https://docs.python.org/2/library/functions.html#isinstance

Moreover, as you (correctly) use the enumerate construct, you already looping over the values with variable v, so no need to access it doing mylist[i].

6 Comments

I had actually forgot an important thing regarding the type of the masked instances. They are numpy.ma.core.MaskedConstant, and not strings. So your approach does not work the way it is now.
My approach should work anyway, since it does not rely on string comparison.
The result I get is a list of 0 values. Nothing else. I don't see why
Please try the second approach
simple typo, was supposed to be "v". Fixed now
|
1

You can use map function here :

>>> list(map(lambda x : 0 if isinstance(x,np.ma.core.MaskedConstant) else 1 , mylist))

[1,1,1,0,0]

Comments

1

You do not see this answer that often therefore I give it despite the other answers are satisfactory. Define a function f

def f(x):
    if isinstance(x, np.ma.core.MaskedConstant):
        return 0
    else:
        return 1

Now do list comprehension

mylist = [f(x) for x in mylist]

2 Comments

Thanks, this is really helpful as it puts things into perspective and reminds me of the existence of functions. However, given the lengthy code I am working on, I find it more suitable to follow a list-comprehension approach.
@CF84 This is the list comprehension approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.