how to write a html to automatically display the dictionary?

Ned Batchelder ned at nedbatchelder.com
Wed Sep 24 01:55:52 CEST 2014


On 9/23/14 4:53 PM, Denis McMahon wrote:
> from string import *

You aren't using any names from string, so you can skip this line.

>
> x={'f1':1,'f2':2,'f3':3}
> y = [ (a,x[a]) for a in x.keys() ]
> y.sort( cmp=lambda a,b: cmp(a[0],b[0]) )

This is more easily done as:

     y = sorted(x.items())

.items() returns a sequence of (key, value) pairs, and tuples are sorted 
lexicographically, so the first elements are compared first, then the 
second, etc.

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list