[Tutor] Sorting a dictionary in one line?

Charlie Clark charlie@begeistert.org
Mon Jan 20 11:14:08 2003


 
> While list comprehension is a redundant construction, it does give a lot 
> in a compact form. If we should remove something today, I can agree with 
> the commonly held opinion that it would be better to through out the old 
> lambda, map, filter and reduce, and keepp list comprehension. (Not that 
> list comprehension replaces reduce or all uses of lambda...)

It's a funny name and I've still got to get used to it. But the Python 
Cookbook has some good illustrations of its use and it makes more sense to 
me than map, lambda and reduce. I'd throw in zip as another thing likely to 
cause confusion.
 
> Why ask when you can run a python interpreter on your computer?
I was asking in the spirit of I know the answer but I'm not sure how we 
arrive at it.

> What do you mean by that? Assign an object to d.keys()? d.keys() returns 
> an object, a list object to be precise. With an assignment, you can make 
> a variable refer to that object. I.e. k = d.keys(). Is that what you 
> meant? I suggest you fire up Python and experiment. It's all fairly 
> logical...

This is exactly the kind of confusion I get into. I had tested stuff in the 
shell before writing it in the mail.
 
> >d = {'b':2, 'c':3, '2'}
> 
> What did you try to do here? A listionary? ;)
hehe
hands up who else frequently types the wrong symbols for the data structure!
This isn't helped by the fact that {[]} all require the modifier key on a 
German keyboard :-(
 
> May I suggest that you sit with the python interpreter available when you 
> write your emails. Type your code in python, not in your mail client, and 
> copy code from the python interpreter to the email. That will reduce the 
> mistypings, and you won't have to ask about things that are obvious if 
> you try for yourself...

point taken.
However, I was trying to recall what you said about the difference between 
references and objects, ie. when what is created.

>From the Terminal:

$ python
Python 2.1.2 (#1, Jan 26 2002, 03:26:04)
[GCC 2.9-beos-991026] on beos5
Type "copyright", "credits" or "license" for more information.
>>> d = {'Magnus':'clever', 'Charlie':'stupid'}
>>> d.keys().sort() # I usually forget the () on keys, values and items
# is keys() a method or an attribute?
>>> a = d.keys()
>>> d['GvR'] = 'BDFL'
>>> a
['Magnus', 'Charlie']
>>> d.keys()
['Magnus', 'Charlie', 'GvR']
>>> a == d.keys() # shows why '=' != '==' in Python!
0   

mm, 'a' is assigned to a list created when the method d.keys() is called? 
ie. d.keys() isn't an attribute of the dictionary although my brain tells 
it's kind of obvious that a dictionary has an index...
so when I change the dictionary 'a' isn't affected because it is referring 
to an object which itself is not actually referencing the dictionary. Is 
this correct? It would seem to be me to be okay for 'a' to be referencing a 
dictionary attribute called keys and for this be continually updated but 
this isn't the case and this is for a reason. I'm just not very sure of the 
reason...

Charlie