[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Better way to negate a boolean list?

David Tremouilles

2/10/2008 7:46:00 AM

Hi,

Is there any better (shorter) way to negate a boolean list than:
>>> negated_boolean_list = [not elem for elem in boolean_list]
?

I tried:
>>> map(not, boolean_list)
but it seems that "not" is not a function.

Thanks in advance,

David
4 Answers

Paddy

2/10/2008 1:17:00 PM

0

On Feb 10, 7:46 am, David Trémouilles <david.t...@gmail.com> wrote:
> Hi,
>
> Is there any better (shorter) way to negate a boolean list than:
> >>> negated_boolean_list = [not elem for elem in boolean_list]
> ?
>
> I tried:
> >>> map(not, boolean_list)
> but it seems that "not" is not a function.
>
> Thanks in advance,
>
> David

Try [not x for x in boolean_list]

- Paddy.

Steve Holden

2/10/2008 1:42:00 PM

0

Paddy wrote:
> On Feb 10, 7:46 am, David Trémouilles <david.t...@gmail.com> wrote:
>> Hi,
>>
>> Is there any better (shorter) way to negate a boolean list than:
>> >>> negated_boolean_list = [not elem for elem in boolean_list]
>> ?
>>
>> I tried:
>> >>> map(not, boolean_list)
>> but it seems that "not" is not a function.
>>
>> Thanks in advance,
>>
>> David
>
> Try [not x for x in boolean_list]
>
This six-character shortening by renaming the comprehension's bound
variable was a joke, right?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Stargaming

2/10/2008 4:27:00 PM

0

On Sun, 10 Feb 2008 08:46:24 +0100, David Trémouilles wrote:
[snip]
> I tried:
> >>> map(not, boolean_list)
> but it seems that "not" is not a function.

`not` is not a function, indeed. It is a keyword, allowing you to write
``not x`` instead of ``not(x)``.

You can of course write a function that just returns its input negated
and pass this function to `map`. Since Python comes with batteries
included, there is such a function already in the `operator module
<http://docs.python.org/lib/module-operato...`_::

import operator
map(operator.not_, boolean_list)
# ^ underscore to distinguish from the keyword

Cheers,

Paddy

2/10/2008 5:31:00 PM

0

On Feb 10, 1:41 pm, Steve Holden <st...@holdenweb.com> wrote:
> Paddy wrote:
> > On Feb 10, 7:46 am, David Trémouilles <david.t...@gmail.com> wrote:
> >> Hi,
>
> >> Is there any better (shorter) way to negate a boolean list than:
> >> >>> negated_boolean_list = [not elem for elem in boolean_list]
> >> ?
>
> >> I tried:
> >> >>> map(not, boolean_list)
> >> but it seems that "not" is not a function.
>
> >> Thanks in advance,
>
> >> David
>
> > Try [not x for x in boolean_list]
>
> This six-character shortening by renaming the comprehension's bound
> variable was a joke, right?
No,
Of course not!
It's much worse - I completely missed the line beginning negated_....
on
first and second reading.
I am indeed getting older, although I had thought I was way off my
dotage.

- Paddy.