0

I have a dataset with two Boolean arrays.

The first boolean array indicates which data points have an error attached to them and is used to specify whether or not to turn on the error in an MCMC routine. The second array indicates whether or not each data point has a measured length.

How can I index the Boolean_error array by the Has_length array, so that I will have a list comprised of objects with Has_length == True with either Boolean_error == True or Boolean_error == False?

Here's an example of what I'm trying to do:

Boolean_error = [False True False True True False True False False False False False]
Has_length = [True True True True False True True True True True True False]

print Boolean_error[Has_length]

>>> [False True False True False True False False False False]
5
  • 1
    What is exactly do you need ? i dont understand Commented Mar 31, 2016 at 20:26
  • Are you missing the commas? Commented Mar 31, 2016 at 20:27
  • note that python has lists and not arrays (well, it does have arrays, but they're uncommon). Also, this is not valid Python Commented Mar 31, 2016 at 20:27
  • 2
    You'll want to use numpy arrays; they have this behaviour. Commented Mar 31, 2016 at 20:29
  • Thank you for the edit, Bharel! I'm a python student, so I'm still learning how to properly phrase things. Commented Apr 1, 2016 at 2:49

2 Answers 2

3

Here you go. That does exactly what you need.

errors_with_length = [b for b, l in zip(Boolean_error, Has_length) if l]

The zip() function takes 1 value from each iterable every time, and the list comprehension filters out the false Has_length, and takes the Boolean_error for those that are True.


Another, more efficient approach would be to use itertools.compress() like so:

>>> list(itertools.compress(Boolean_error, Has_length))
[False, True, False, True, False, True, False, False, False, False]
Sign up to request clarification or add additional context in comments.

Comments

0

You could just write a function to do this.

def return_list_with_only_known_length(error, length):
    output = []
    for i, b in enumerate(length):
        if b:
            output.append(error[i])
    return output

1 Comment

Long and complicated mate. Edited your answer so it will be a bit more tidy but there's still an easier answer.

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.