search
menu
person

NEWS AND UDATES


This question has already been answered, but because it's popular on Google, here's what Python's documentation uses:

Код
>>> from collections import OrderedDict

>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}

>>> # dictionary sorted by key -- OrderedDict(sorted(d.items()) also works
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[ ... Читать дальше »
Просмотров: 1334 | Добавил: django | Дата: 24.09.2013 | Комментарии (0)