[Tutor] problem with building dict w/ SQlite SELECTS in loop

John Fouhy john at fouhy.net
Wed Sep 17 00:11:06 CEST 2008


2008/9/17 Che M <pine508 at hotmail.com>:
>> (heck, you could select code, start, style form codes -- pull all the
>> information you need in a single query, and skip the loop
>> altogether..)
> I think I need the loop because the style will be multiple styles and
> I need to take the codes that go with each style, so I am querying
> style by style, so to speak.

What I meant is that you could do this:

cur.execute("select code, start, style from codes where code != '' and
start > ? and start <= ?", #etc)
results = cur.fetchall()
self.style_data_dict = {}
for code, start, style in results:
  self.style_data_dict.setdefault(style, []).append((code, start))

This will leave your data dict in a different form from the one in
your original code.. but you could change it by:

for style in self.style_data_dict:
  self.style_data_dict[style] = zip(*self.style_data_dict[style])

-- 
John.


More information about the Tutor mailing list