Re: Anyway to clarify this code? (dictionaries)

2005-11-23 Thread Bengt Richter
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

Re: Anyway to clarify this code? (dictionaries)

2005-11-23 Thread Steven D'Aprano
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?

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread [EMAIL PROTECTED]
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

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread Mike Meyer
"[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

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread [EMAIL PROTECTED]
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

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread [EMAIL PROTECTED]
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

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread Bengt Richter
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

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread [EMAIL PROTECTED]
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

Re: Anyway to clarify this code? (dictionaries)

2005-11-22 Thread Mike Meyer
"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

Anyway to clarify this code? (dictionaries)

2005-11-22 Thread javuchi
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