list organization question
John Machin
sjmachin at lexicon.net
Thu Dec 11 18:43:41 EST 2008
On Dec 12, 10:17 am, Robocop <btha... at physics.ucsd.edu> wrote:
> I'm currently trying something along the lines of a sort.compare, but
> as i'm never sure how many mini-lists i'll end up with, i'm not sure
> how exactly to begin. Maybe something like a C vector, i.e. a list of
> pointers to other lists? Or more specifically, compare dates in my
> list, push that into some empty dates[], then do something along the
> lines of for looping over dates to create subset lists, and nesting
> some more compares within these lists to further sort the data by id.
> Sound crazy or plausible?
Crazy, yes. Plausible that you might attempt it, yes.
Learn about dictionaries. They're built in; you don't need to import
them.
E.g. [OTTOMH; untested]
id_map = {} # maps (unique) id to the object with that id
date_map = {} # maps date to a list of ids whose objects have that
date
for o in olist:
if o.id in id_map:
raise Exception("duplicate id: %r" % o.id)
id_map[o.id] = o
if o.date not in date_map:
date_map[o.date] = []
date_map[o.date].append(o.id)
More information about the Python-list
mailing list