gnumed-devel
[Top][All Lists]
Advanced

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

Re: [Gnumed-devel] Dictionary Question


From: Horst Herb
Subject: Re: [Gnumed-devel] Dictionary Question
Date: Fri, 18 Feb 2005 07:08:47 -0500
User-agent: KMail/1.7.1

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.

> This looks like a dictionary structure but has a 'u' character before each
> string?

The "u" symbolizes a unicode string, meaning every character you see is stored 
as a 16bit unicode value. (instead of a string object, you have a unicode 
object, which for practical purposes will behave the same most of the time)




reply via email to

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