[SciPy-user] what is simpliest way to create this boolean array

Angus McMorland amcmorl@gmail....
Thu Aug 30 04:33:29 CDT 2007


On 30/08/2007, dmitrey <openopt@ukr.net> wrote:
> No, I meant a = array((True, True, ... True (m1 numbers), False,
> False,... False(m2 numbers), True, ...(m3 numbers), False, ...(m4
> numbers) ))

Of course, that makes more sense. A cool generic list comprehension solution is:

def make_bool(*args):
    a = []
    [[a.append(k) for k in [not bool((i % 2)) for y in xrange(x)]] for
i, x in enumerate(args)]
    return a

Still slightly faster than the numpy way (by ~25% I think).

> If I'll get nothing better than Stéfan propose I'll use the way
> a = array(ones(m1+m2+m3+m4), bool)
> a[m1:m1+m2]=False
> a[m1+m2+m3:]=False
> D
>
>
>
> Angus McMorland wrote:
> > On 30/08/2007, dmitrey <openopt@ukr.net> wrote:
> >
> >> hi all,
> >> what's the easiest way to create bool array that contains m1 True, m2
> >> False, m3 True, m4 False?
> >> Or (alternatively) creating same Python list instead.
> >>
> >
> > How about "n.array([bool(1 - (x % 2)) for x in xrange(length)])"?
> >
> > A.
> >
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>


-- 
AJC McMorland, PhD Student
Physiology, University of Auckland


More information about the SciPy-user mailing list