Duncan Booth wrote: > [EMAIL PROTECTED] wrote: > > > As for the (k,v) vs (v,k), I still don't think it is a good example. I > > can always use index to access the tuple elements and most other > > functions expect the first element to be the key. For example : > > > > a=d.items() > > do something about a > > b = dict(a) > > > > But using the imaginary example : > > > > a = zip(d.values(), d.keys()) > > do something about a > > b = dict(a) # probably not what I want. > > > > The typical use case for the (v,k) tuple would be when you want sort the > contents of a dictionary by value. e.g. counting the number of occurrences > of each word in a document. > > a = zip(d.values(), d.keys()) > a.sort() > a.reverse() > print "Today's top 10:" > print str.join(',', [ k for (v,k) in a[:10]]) > > Ok, so today you can do that another way, but before there was a key > parameter to sort you had to build the tuple somehow: > > print str.join(',', sorted(a.iterkeys(), > key=a.__getitem__, reverse=True)[:10]) > > and the advantage of the latter is of course that it works even when you've > been counting the number of occurences of complex numbers in a document :)
Thanks, so for newer python(2.3+?, don't know when the sort has been enhanced), this (v,k) is again there for historical reason but not a real use case any more. -- http://mail.python.org/mailman/listinfo/python-list