On 22 Nov 2005 19:52:40 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>
>Bengt Richter wrote:
>> >>> def my_search(another, keys, x): return dict((k,another[k]) for k in
>> keys if another[k]>x)
>> ...
>> >>> my_search(another, 'cb', .3)
>> {'b': 0.35806602909756235}
>> >>> my_search
javuchi wrote:
> I want to avoid converting the dictionary to a list and then to a
> dictionay. Are there speed penalties for such a conversion?
You mean, is it faster to write, test, debug and
execute slow Python code rather than letting Python's
built-in routines written in fast C do the job?
Mike Meyer wrote:
> First, remember the warnings about premature optimization.
Which is why I said the one-liner(your first one) is clean and clear,
and bug free in one go.
>
> use = set(another) - set(keys)
> return dict([[k, another[k]] for k in use if another[k] >= x]
>
> Though I
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> 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 pr
Bengt Richter wrote:
> >>> def my_search(another, keys, x): return dict((k,another[k]) for k in
> keys if another[k]>x)
> ...
> >>> my_search(another, 'cb', .3)
> {'b': 0.35806602909756235}
> >>> my_search(another, 'abcd', .4)
> {'a': 0.60649466203365532, 'd': 0.77440643221840166}
>
Do you
Mike Meyer wrote:
> def my_search(another, keys, x):
> new = dict()
> for k in keys:
> if another[k] >= x:
> new[k] = another[k]
> return new
>
BTW, this would raise exception if k is not in another.
--
http://mail.python.org/mailman/listinfo/python-list
On 22 Nov 2005 17:58:28 -0800, "javuchi" <[EMAIL PROTECTED]> wrote:
>I've been searching thru the library documentation, and this is the
>best code I can produce for this alogorithm:
>
>I'd like to return a dictionary which is a copy of 'another' dictionary
>whoes values are bigger than 'x' and ha
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 inde
"javuchi" <[EMAIL PROTECTED]> writes:
> I've been searching thru the library documentation, and this is the
> best code I can produce for this alogorithm:
>
> I'd like to return a dictionary which is a copy of 'another' dictionary
> whoes values are bigger than 'x' and has the keys 'keys':
>
> def
I've been searching thru the library documentation, and this is the
best code I can produce for this alogorithm:
I'd like to return a dictionary which is a copy of 'another' dictionary
whoes values are bigger than 'x' and has the keys 'keys':
def my_search (another, keys, x):
temp = anoth
10 matches
Mail list logo