simultaneous assignment

Grant Edwards grante at visi.com
Tue May 2 15:57:14 EDT 2006


On 2006-05-02, Boris Borcic <bborcic at gmail.com> wrote:
> Grant Edwards wrote:
>> Python knows how to count.  :)
>> 
>> def countFalse(seq):
>>     return len([v for v in seq if not v])
>> 
>> def countTrue(seq):
>>     return len([v for v in seq if v])
>> 
>> def truth_test(seq):
>>     return countTrue(seq) == 1
>> 
>
> I'd suggest the more direct
>
> def countFalse(seq) :
>      return sum(1 for v in seq if not v)

I guess I don't see how that is more direct.

If you want to know how many items are in a seqneuce, you call
len().  

That's what it's for.

The number of objects in a list is returned in O(1) time by
len(sequence). 

Converting that list to a list of 1's and then summing the 1's
is O(N).

-- 
Grant Edwards                   grante             Yow!  Actually, what
                                  at               I'd like is a little toy
                               visi.com            spaceship!!



More information about the Python-list mailing list