Hello -------- Original Message -------- From: Kent Johnson <ken...@tds.net> Apparently from: kent3...@gmail.com To: davidwil...@safe-mail.net Cc: tutor@python.org Subject: Re: [Tutor] dictionaries help Date: Thu, 23 Jul 2009 21:44:33 -0400
> On Thu, Jul 23, 2009 at 7:09 PM, <davidwil...@safe-mail.net> wrote: > > Hello again, > > Here is my full attempt, can you please tell me if it is OK and if there > > should be any improvements > > > >>>> ws_industry = ('it', 'science') > >>>> code = ws_industry[0] > >>>> active = [] > >>>> industries = [{'code': 'it', 'name': 'Information Technology'}, {'code': > >>>> 'science', 'name': 'Science'}, {'code': 'biology', 'name': 'Biology'}] > >>>> for industry in industries: > > if industry['code'] in ws_industry: > > active.append({ > > 'code': industry['code'], > > 'name': industry['name'], > > 'isdefault': code == industry['code']}) > > > >>>># Not active industry > >>>> not_active = [x for x in industries if x['code'] not in ws_industry ] > >>>>not_active.sort(lambda x, y: cmp(x['name'], y['name'])) > >>>> return [active, non-active] > > > > Line [6] i was not too sure if it could be written better or more efficient. > > > > Basically I have a big list of industries and I want to verify for each > > member whether they are active or not active. > > > > The industry list of dictionaries will stay fixed at about 25 items, > > whereas the ws_industry tuple will change depending. I have to go through > > about 20,000 items. > > If ws_industry contains 25 items, it will probably be faster to search > it if it is a set rather than a list. Just use > ws_industry = set(('it', 'science')) ws_industry contains only unique values > > Which part of the problem has 20,000 items? the 20,000 items are the number of times the programme will have to loop to extract this information. so i have 25 industries, i also have 20,000 text files which contain the ws_industry values therefore i would like to extract from each file the number of active industries and number of inactive industries. i suppose in a relational db i will have something like: [file] 1 - n [ws-industry] n - n [industry] hope this make sense ;) > > Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor