如何在Python中以正确的方式对字典列表进行排序和分组

时间:2018-09-14 05:02:22

标签: python django django-templates

我在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

有什么建议吗?

2 个答案:

答案 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 %}