Newbie Questions: Swithing from Perl to Python
Robert Brewer
fumanchu at amor.org
Sat Oct 25 21:07:13 EDT 2003
Luther Barnum wrote:
> 2. How can I sort and print out a hash.
>
> Example in Perl:
>
> ex. foreach $string (sort keys %hash) {
> print("$string = $hash{$string}\n");
> }
>
>>> myhash = {'a': 1, 'b': 2, 'c': 3}
>>> myhash
{'a': 1, 'c': 3, 'b': 2}
>>> sortedKeys = myhash.keys()
>>> sortedKeys.sort()
>>> for eachKey in sortedKeys:
... print "%s = %s\n" % (eachKey, myhash[eachKey])
...
a = 1
b = 2
c = 3
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
More information about the Python-list
mailing list