Steven D'Aprano wrote:
Colin J. Williams wrote:

Sometimes, it's useful to be able to
obtain the data in the sorted sequence.

You might consider adding functionality
like:

def seqItems(self):
'''To return the items, sorted
by key. '''
return [self[k] for k in
self.seqKeys()]

Amazingly, the Python time-machine provides such functionality! Using just a
handful of pre-existing pieces, you can do this:

sorted(mydict.items())
sorted(mydict.keys())
[mydict[x] for x in sorted(mydict.keys)]

and any other sequence you might need. That's the beauty of a general
purpose programming language. You don't need everything to be a built-in
*wink*


Thanks, you are right, you have a neat way of handling the first two, they work with an instance of Raymond Hettinger's. OrderedDict.

The third suggestion has a couple of problems, which are fixed below.

if __name__ == '__main__':
d= OrderedDict.fromkeys('abracadabra', value= 'zzz')
    print(d)
    print(d.seqKeys())
    print(d.seqItems())
    print(d.seqValues())
    # Steven D'Aprano's alternative
    mydict= d.copy()
    print sorted(mydict.items())
    print sorted(mydict.keys())
# print [mydict[x] for x in sorted(mydict.keys)] Instance object is not iterable print(sorted(iter([(x[1], x[0]) for x in mydict.iteritems()]))) # This works

Colin W.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to