How to test if one dict is subset of another?

Paul Rubin http
Tue Feb 20 03:12:48 EST 2007


"Jay Tee" <jeff.templon at gmail.com> writes:
> for j in jobs:
>    if (j.get('user') == 'jeff' and j.get('state')=='running') :
>       do_something()

Sounds like you need some backing data structures, like indexes
in a database, e.g. (untested, uses the cool new defaultdicts of 2.5):

   index = defaultdict(lambda: defaultdict(set))
   for j in jobs:
     for k in j's dict:
        index[k][j.get(k)].add(j)

Now for

>    if j.subset_attr({'user' : 'jeff', 'state' : 'running'}) :
>       do_something()

you'd just write:

    for j in (index['user']['jeff'] & index['state']['running']):
        do_something()



More information about the Python-list mailing list