[Tutor] Sorting the Dictionary Set?
Ken G.
beachkid at insightbb.com
Tue Jul 6 16:38:55 CEST 2010
Is there a way to sort a dictionary?
Assuming I have a dictionary set containing the following:
{'02': 1, '03': 1, '12': 1, '15': 2, '14': 2, '04': 3, '05': 1, '19': 1,
'32': 1, '28': 1, '27': 1, '17': 2, '25': 1}
and using the following code:
print ('Printing the result of numbers that are repeated:')
print
for x, y in counted.items():
if y > 1:
print "Number %s was found %s times." % (x,y)
# else:
# print "Number %s was found %s times." % (x,y)
print
and the result are:
Printing the result of numbers that are repeated:
Number 15 was found 2 times.
Number 14 was found 2 times.
Number 04 was found 3 times.
Number 17 was found 2 times.
and the question is:
How do I sort the dictionary so the numbers listed (15, 14, 04, 17) are
listed in sorted ascending order such as:
Number 04 was found 3 times.
Number 14 was found 2 times.
Number 15 was found 2 times.
Number 17 was found 2 times.
I would appreciate any help, suggestion or hint on how to do achieved
the desired result.
Thanks,
Ken
More information about the Tutor
mailing list