Anyway to clarify this code? (dictionaries)

bonono at gmail.com bonono at gmail.com
Tue Nov 22 22:33:36 EST 2005


Mike Meyer wrote:
> def my_search(another, keys, x):
>    return dict([[k, v] for k, v in another.items() if v >= x and k in keys])
>
> But then you're looking through all the keys in another, and searching
> through keys multiple times, which probably adds up to a lot more
> wasted work than indexing another twice.
Would you mind clarify ? Do you mean "k in keys" is a scan rather than
a lookup ? I find it to be pretty clean and straight forward.

I think one way or another, one need to loop through one of them, then
index search the other. It may help a bit to take the len() and loop
through the shorter one.

This seems like a SQL equivalent.

select * from a where a.key=b.key and a.v >= x




More information about the Python-list mailing list