[EMAIL PROTECTED] wrote:
You cannot rely on the elements of a dictionary being in any
particular order (dicts are internally hash tables), so the above
is almost certainly ont what you want.


Hi - thanks for your reply.  How about if I made the dict into a list
(of
which I have done).  How would I then reference the previous item?
Can they
be indexed?

Yes, I ran in a situation similar to yours, I read the content of a file, and had to both keep the order of the lines in the original file, and create a dictionary from the lines. So I created a class that contained both a dictionary and a tuple containing the keys of the dictionary in the original order. Something like this:

class mydict(object):

  def __init__(self, filename):
    x = [ e.strip().split() for e in file(filename) ]
    self.ordered_lines = tuple([ e[0] for e in x ])
    self.dictionary = dict( zip(self.ordered_lines, [ e[1:] for e in x]) )

a = mydict('/some/file')
a.ordered_lines
a.dictionary



Yves.
http://www.SollerS.ca
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to