我在Python中有一个字典列表,如下所示:
options = [
{'group_priority': 2, 'group': u'Comfort', 'name': u'Air conditioning'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Air suspension'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Cruise control'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Leather interior'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Parking assist system camera'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Parking assist system sensors rear'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Power steering'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Seat heating'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Sunroof'},
{'group_priority': 3, 'group': u'Entertainment', 'name': u'Bluetooth'},
{'group_priority': 3, 'group': u'Entertainment', 'name': u'MP3'},
{'group_priority': 3, 'group': u'Entertainment', 'name': u'Navigation system'},
{'group_priority': 4, 'group': u'Extra', 'name': u'Alloy wheels'},
{'group_priority': 4, 'group': u'Extra', 'name': u'Trailer hitch'},
{'group_priority': 4, 'group': u'Extra', 'name': u'Winter tyres'},
{'group_priority': 4, 'group': u'Security', 'name': u'Xenon headlights'}]
我想按group_priority
排序并按group
分组并以Django模板显示
我尝试过的事情:
options_grouped_dict = {}
for item in options_grouped:
options_grouped_dict.setdefault(item['group'], []).append(item['name'])
然后在Django模板中:
{% for key, values in car.options_grouped.items %}
<div>
<div><strong>{{key}}</strong></div>
{% for option in values %}
<div>
{% if option|length > 31 %}
{{ option|truncatechars:34 }}
{% else %}
{{ option }}
{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
但是group_priority
并未对其进行排序。我首先获得security
组,而不是Comfort
有什么建议吗?
答案 0 :(得分:1)
如果您希望使用由group
分组并由group_priority
排序的字典的orderedDict,则应该可以:
#grouping in Dict with group as key and list of dict as value:
grouped_dict={}
for option in options:
if option['group'] in grouped_dict.keys():
grouped_dict[option['group']].append(option)
else:
grouped_dict[option['group']] = [option]
#sorting by group_priority
ordered = OrderedDict(sorted(grouped_dict.items(), key=lambda t:t[1][0].get('group_priority')))
print(ordered)
答案 1 :(得分:1)
我使用Django的regroup解决了这个问题。
选项列表如下:
options = [
{'group_priority': 2, 'group': u'Comfort', 'name': u'Air conditioning'},
{'group_priority': 2, 'group': u'Comfort', 'name': u'Air suspension'},
{'group_priority': 3, 'group': u'Entertainment', 'name': u'Bluetooth'},
{'group_priority': 3, 'group': u'Entertainment', 'name': u'MP3'},
{'group_priority': 3, 'group': u'Entertainment', 'name': u'Navigation system'},
{'group_priority': 4, 'group': u'Extra', 'name': u'Alloy wheels'},
{'group_priority': 4, 'group': u'Extra', 'name': u'Trailer hitch'},
{'group_priority': 4, 'group': u'Security', 'name': u'Xenon headlights'}]
我已将它(options
)传递给模板,如下所示car['options_grouped'] = options
然后在模板中,我按如下操作:
{% regroup car.options_grouped by group as groups %}
{% for group in groups %}
<div>{{ group.grouper }}</div>
{% for option in group.list %}
<div> {{ option.name }}</div>
{% endfor %}
{% endfor %}