Re: [Tutor] index of a list of lists of strings
Magnus Lycka
magnus at thinkware.se
Wed May 12 15:19:57 EDT 2004
tpc at csua.berkeley.edu wrote:
> hi everybody, let's say I have a list called donorValuesList that stores
> lists of strings, such that donorValuesList[0] would return ['NULL',
> 'NULL', 'James', 'NULL', 'Smith' ... etc] and donorValuesList[1] would
> return ['NULL', 'Mr', 'William', 'NULL', 'Johnson' ... etc]. Could I
> find the index of a specific list if I knew only a few of the strings and
> their positions ? For example, I want to find the index in
> donorValuesList of a list where 'John' is the third value, 'NULL' is the
> fourth, and 'Brown' is the fifth, but I don't have enough information to
> construct a complete list to pass to donorValuesList.index(). Is this
> possible ?
Of course it's possible. You simply loop through the list and test each
record. I'm not sure it's an optimal solution though.
First of all, there is a particualar value in Python called None. I
assume 'NULL' is supposed to indicate that a value is missing, but one
of these days you will run into a Mr Null who prefers his names to be
written in all caps... With Python's dynamic typing, there is no reason
to abuse strings or integers by letting particular values such as 'NULL'
or 99999 have exceptional meanings. So, it's better to use something like:
[None, None, 'James', None, 'Smith' ... etc]
instead of
['NULL', 'NULL', 'James', 'NULL', 'Smith' ... etc]
The None value is never a string, it's just itself. It's clear that its
noones name.
I'm not sure a list of list is the data structure you really need though.
If you want a pure Python solution, you are probably better off making a
Donor class, and a DonorCatalog class which can handle queries.
Another option that seems to fit your requirements is to store the data
in a relational database. For a small database which doesn't require any
server setup etc, look at sqlite. See http://pysqlite.sourceforge.net/
Then you create a table roughly like this:
CREATE TABLE DONOR (
ID INT NOT NULL PRIMARY KEY,
SOMETHING SOME_TYPE,
SALUTATION CHAR(5),
FIRST_NAME CHAR(30),
LAST_NAME CHAR(30),
ETC SOME_TYPE
)
Once you've populated your table, you can search it for people with the
surname 'Brown' like this:
SELECT * FROM DONOR WHERE LAST_NAME = 'Brown'
All this can be wrapped in Python, either by using SQL inside
cursor.execute() as per the Python DB-API, or you can "hide" all
these SQLities behind classes using an OO-RDBMS wrapper such as
SQLObject. See http://sqlobject.org/ Then you'd query your database
like this:
donors_called_brown = Donor.select(Donor.q.lastName='Brown')
I realize that it might be a bit much to learn SQL as well if
programming is new for you, but relational databases are much
more convenient for the kind of ad hoc searches you seem to do,
and they provide persistence and scalability in a neat package.
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus at thinkware.se
More information about the Tutor
mailing list