By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

why should dict not be callable?

P: n/a
A couple of times recently I've come across this problem: I have a
large list to sort and I need to the the "key=function" argument to
sort appropriately. But I actually have the key mapping in a big
dictionary. Now I have to make an intermediary function:

def key_fn(key):
return key_dict[key]

If a dict were callable directly, this would be unnecessary. Often the
small extra complexity and compute time is not important, but in
sorting a large list, it could be a significant difference. Is there
some reason a subscriptable thing like a dict should *not* be callable?
-- George [nitpicking...]

Oct 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
georgeryo...@gmail.com wrote:
A couple of times recently I've come across this problem: I have a
large list to sort and I need to the the "key=function" argument to
sort appropriately. But I actually have the key mapping in a big
dictionary.
Is this what you mean? I suppose the lambda is an "intermediary
function" ... but meh...

l = ['bob', 'dog', 'cat', 'apple']
d = {'bob': 7, 'dog': 0, 'cat': 14, 'apple': 3}

l.sort(lambda x,y: d[x] - d[y])

print l
['dog', 'apple', 'bob', 'cat']

Oct 17 '06 #2

P: n/a
ge**********@gmail.com wrote:
A couple of times recently I've come across this problem: I have a
large list to sort and I need to the the "key=function" argument to
sort appropriately. But I actually have the key mapping in a big
dictionary. Now I have to make an intermediary function:

def key_fn(key):
return key_dict[key]
Try key=key_dict.__getitem__

In [3]: d=dict(a=1,b=2)

In [4]: d.__getitem__('a')
Out[4]: 1

Kent
Oct 17 '06 #3

P: n/a
ge**********@gmail.com wrote:
A couple of times recently I've come across this problem: I have a
large list to sort and I need to the the "key=function" argument to
sort appropriately. But I actually have the key mapping in a big
dictionary. Now I have to make an intermediary function:

def key_fn(key):
return key_dict[key]

If a dict were callable directly, this would be unnecessary. Often the
small extra complexity and compute time is not important, but in
sorting a large list, it could be a significant difference. Is there
some reason a subscriptable thing like a dict should *not* be callable?
Because you can pass key_dict.get instead. Or even key_dict.__getitem__ if you
don't want the default=None behavior of the .get() method.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 17 '06 #4

P: n/a
ge**********@gmail.com wrote:
A couple of times recently I've come across this problem: I have a
large list to sort and I need to the the "key=function" argument to
sort appropriately. But I actually have the key mapping in a big
dictionary.
so use a bound method:

L.sort(key=key_dict.get)

</F>

Oct 17 '06 #5

P: n/a
On Oct 17, 3:37 pm, Fredrik Lundh <fred...@pythonware.comwrote:
georgeryo...@gmail.com wrote:
A couple of times recently I've come across this problem: I have a
large list to sort and I need to the the "key=function" argument to
sort appropriately. But I actually have the key mapping in a big
dictionary.so use a bound method:

L.sort(key=key_dict.get)
Duh! Why didn't I think of that... Thanks guys, that's perfect.

-- George

Oct 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.