Identify and perform actions to data within stated limits

M.E.Farmer mefjr75 at hotmail.com
Tue Mar 1 17:34:00 EST 2005


dasacc at gmail.com wrote:
> I found out that doing a re.findall will split up the results into an
> array, or rather something called a list (looks like an array to me).
> I'd be set if i could just count the elements in the array but I
can't
> seem to find anything in the documentation on how to : /  ...
Hello,
List have methods as do most other built-in objects in Python.
If you want to know what methods an object has just dir() it.
Py> q = [1,2,3,4,5,6,7,8]# create a  list
Py> dir(q)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__repr__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__str__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort']
The exact output depends on your Python version.
Notice that there are methods called count and index.
Open an interpreter and try playing with them and see what they do.
If you weant to know what an object or methods docs say, use help().
Py> help(q.count)
Help on built-in function count:

count(...)
    L.count(value) -> integer -- return number of occurrences of value

If you want to know the number of items in a list ( or most builtins )
use len().
Py> len(q)
8
Spend time reading the docs. It will save you months of work.
Also learn the library it has many modules that will simplify your code
and save your mind.
http://www.python.org/doc/
hth,
M.E.Farmer




More information about the Python-list mailing list