[GLLUG] The Python talk reminds me of a question...

Dave Rogers DaveRogers at stockalerts.com
Sun Aug 15 23:05:43 EDT 2004


Ashton,

You could, at the least, try using a dictionary, or really, a list of 
dictionaries.

db = [{'Last':'Smith', 'First': 'Jane C', .....'Title' : 'Great ponies 
of Ohio' }, {....} ]

Then you could go:

for record in db:
    print record['Title']

That would give you all the Title values of each dictionary.

The list approach would work with this code:

for record in db:
    print record[4]

but, 'Title" is so much easier to read and understand than 4.  
Dictionaries are great data structures.  They have handy methods like 
keys(), has_key(), etc....

Good luck, Dave

Ashton Shortridge wrote:

>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
>
>  
>



More information about the linux-user mailing list