On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot <[email protected]> wrote:
>>>> sorted(a.items(), key=a.get)
> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3',
> datetime.datetim
> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, 12,
> 17,
> 29, 100))]
>>>>
>
> However, trying to do the same thing from the script does not sort the
> dictionary:
>
> sorted(my_dict.items(), key=my_dict.get, reverse=False)
> for key, value in my_dict.items():
> print value, key
>
> the dictionary prints with unsorted items.
The sorted() function returns a sorted list. You're then going back to
the original dictionary. Instead, just iterate over the sorted items:
items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
for key, value in items:
print value, key
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list