【发布时间】:2014-02-11 04:21:24
【问题描述】:
我有这个:
order = [
'foo',
'bar',
'baz',
]
data = {
'bar': 'barValue',
'foo': 'fooValue',
'baz': 'bazValue',
}
我想最后拥有这个:
sorted_data = [
'fooValue',
'barValue',
'bazValue',
]
所以,我有一个带有键值对的 dict,我有一个列表,其中包含该 dict 中按某种顺序排列的键。我想得到一个字典值的列表,按它们在列表中指定的顺序排序。
我需要这个,因为不幸的是 Django 在模板中不支持像这样简单的东西:
{% for key in order %}
<div>{{ data[key] }}</div>
{% endfor %}
【问题讨论】:
-
这不叫
sorting
标签: python django python-2.7 django-templates