[GLLUG] The Python talk reminds me of a question...
Ashton Shortridge
ashton at msu.edu
Sun Aug 15 13:11:38 EDT 2004
Hello,
I've been coding in C for a decade now, and, while I'm not particularly
skilled, I am able to hack out stuff that I need fairly quickly.
However, times change and I have decided to learn Python. I've skimmed the
tutorial and other information sources, but to really get into it I figured
the best approach was to decide on a little project and implement it with
Python.
My project is to implement a bibliography program - basically a little
database with journal articles, book chapters, etc, that I can ultimately
toss.
My first approach is purposefully not object-oriented. A few lines of code is
sufficient to import the records from a text file (very cool that it's so
easy - would have taken much more effort in C!). I am using a list (really a
list of lists), which is probably not the best, or possibly even a good, way
to do it. The structure looks something like this:
db = [['Smith', 'Jane C', '', '', 'Great ponies of Ohio'],['Xie', 'Fang', '',
'', 'Mane Coloration']]
In the example above there are two records. Each record has 5 'fields',
including two that are empty (for a second author, if any).
I would like to be able to do something with the records based on the title
field - item 4 in each of the records. I can return this for the second
record using the following syntax:
db[1][4]
will return 'Mane coloration'
Here comes the question. I want to return ALL of the titles. Intuitively (but
wrongly), I tried:
db[:][4]
which returns an error that basically says there is no 4th element in the list
- meaning the list called db, which only contains two things. Other stabs
also met with failure.
So, the question is, how can I refer to the nth elements of ALL records? If
the answer is: "hey silly, use a different data structure!" , then just tell
me about that structure. And if there's a web reference about it (I've looked
over the official Python documentation with some care and a lot of
confusion), I'd appreciate learning that too.
The next step by the way will be to create some sort of record object and
build version II of the application. While both will be open source, don't
look for it in your distro any time soon!
Thanks,
Ashton
--
Ashton Shortridge
Assistant Professor ashton at msu.edu
Dept of Geography http://www.msu.edu/~ashton
Michigan State University (517) 432-3561
More information about the linux-user
mailing list