473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why should dict not be callable?

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
5 2431
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Roy Smith | last post by:
I've got a function which gets passed a call-back function as a parameter. I want to check to make sure the thing passed in is indeed callable. Is: assert '__call__' in dir (param) the...
15
by: Irmen de Jong | last post by:
Hi I have this dict that maps a name to a sequence of other names. I want to have it reversed, i.e., map the other names each to the key they belong to (yes, the other names are unique and they...
25
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that...
11
by: cjl | last post by:
Hey all: I want to convert strings (ex. '3', '32') to strings with left padded zeroes (ex. '003', '032'), so I tried this: string1 = '32' string2 = "%03s" % (string1) print string2 >32
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
6
by: Ron Garret | last post by:
If I do this: def f(self): print self class c1: pass setattr(c1, 'm1', f) Then f is automagically transmogrified into the appropriate sort of method depending on how it is used:
25
by: Alex Popescu | last post by:
Hi all! I am pretty sure this has been asked a couple of times, but I don't seem to find it on the archives (Google seems to have a couple of problems lately). I am wondering what is the most...
1
by: Astan Chee | last post by:
Hi, I have a variable, I want to check if it is a dictionary or a string. Is there any better way to do this than I've done. How I did it is by doing a .items() and catching a AttributeError that...
2
by: ssecorp | last post by:
I did nce(I think). class X X.__dict__() and ngot a dict of its variables. Now i get errors doing this. what am i doing wrong?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.