simple question:the count of array

Andy Todd andy_todd at spam.free.yahoo.com
Tue Jul 31 22:07:01 EDT 2001


Simon Brunning <SBrunning at trisystems.co.uk> wrote in
<mailman.996583798.16808.python-list at python.org>: 

>> From:     sdf [SMTP:wqh-2 at 263.net]
>> >>> a=['a','b','c']
>> >>> b=a.count()
>> Traceback (most recent call last):
>>   File "<pyshell#9>", line 1, in ?
>>     b=a.count()
>> TypeError: count() takes exactly 1 argument (0 given)
>> >>> 
>> 
>> then how I get the number of a,I mean how much 
>> values in a,in this case it should be 3 
> 
>>>>b=len(a)
>>>>print b
>3
>
>Cheers,
>Simon Brunning
>TriSystems Ltd.
>sbrunning at trisystems.co.uk
>
>
>
>
thanks for the simple answer Simon, and for those of use who reached for 
the interpreter to find out what this does;

>>>b=a.count()

see section 2.1.5.4 of the documentation (Mutable Sequence Types), which 
says;

"""
s.count(x) return number of i's for which s[i]==x
"""

so, another alternative to what you were trying would be;

>>> a=['a','b','c']
>>> b=a.count('b')
>>> print b
1
>>> x=a.count('x')
>>> print x
0

HTH,
Andy
-- 
Content free posts a speciality



More information about the Python-list mailing list