gnumed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gnumed-devel] Dictionary Question


From: David Grant
Subject: Re: [Gnumed-devel] Dictionary Question
Date: Sun, 20 Feb 2005 17:52:05 -0800
User-agent: Mozilla Thunderbird 1.0 (X11/20041221)

Horst Herb wrote:

On Fri, 18 Feb 2005 04:18, Richard Terry wrote:
mydict = {}
mydict[0] = line1, line2, line3, line4, line5

if I print out mydict (as you can see I put drug stuff in it) I get this:

{0: (u'', u'omeprazole', u'omeprazole magnesium', u'Losec Tablets',
u'20mg', u'1 mane')}

If you really want to make use of a dictionary data type, you should do like
mydict['drug']='omeprazole'
mydict['strength']='20mg'

What you are doing is using a dictionary as if it was a simple list.
Lists are faster and consume less memory, might even be appropriate.
IN that case you would declare
mylist = []

if "o" is your primary key in the example above, and you want to hold the data for multiple primary keys in memory, you can use a dictionary of dictionaries, or a dictonary of lists, or lists of dictionaries etc.

Main difference:
A dictionary is an *unordered* collection of objects. objects are accessed via a "key" A list is an *ordered* collection of objects which are accessed through a numeric index In Python you have a third similar datatype called a "tuple". This is essentially the same as a list, only that it is immutable (you cannot add objects to a tuple, nor delete or modify objects within the tuple). Tuples provide the fastest and least memory consuming access to object collections.

There are also sets, which are a module in python 2.3 and built-in to python 2.4. It is like a dictionary but there are no values. Just a bunch of keys. So it's basically a list, where all the elements stored in the list are unique. It was useful for what I was doing which was searching a file, and storing all word occurrences. But there are many occurrences of words (like "the" "and" and so forth) and I wasn't interested in counting occurrences, just knowing which words were present. If you try to add "and" to the set many times, you just get one of them stored.

--
David J. Grant
http://www.davidgrant.ca:81

Attachment: david.grant.vcf
Description: Vcard


reply via email to

[Prev in Thread] Current Thread [Next in Thread]