[Tutor] dictionaries help
Kent Johnson
kent37 at tds.net
Fri Jul 24 03:44:33 CEST 2009
On Thu, Jul 23, 2009 at 7:09 PM, <davidwilson at 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'))
Which part of the problem has 20,000 items?
Kent
More information about the Tutor
mailing list