在jinja2循环中对dict进行排序

Syl*_*Syl 17 python jinja2 flask

我还在学习jinja2和flask,我在jinja2中使用dictsort时遇到了困难.

所以我将这个字典传递给jinja2模板:

{'PEDD United': {'id': 37828, 'rank': 12, 'totalpts': 307},'Fc Mbonabushia': {'id': 205633, 'rank': 6, 'totalpts': 356},'FC Slurp': {'id': 933573, 'rank': 11, 'totalpts': 312},'KFC_Overijse': {'id': 38861, 'rank': 5, 'totalpts': 362},'Fc Paris': {'id': 1538051, 'rank': 2, 'totalpts': 396}}
Run Code Online (Sandbox Code Playgroud)

我想要的是创建一个表,该表按键'totalpts'的值排序.我尝试了各种各样的东西,它只是在"排序"时不考虑总计.

这是我的一个代码:

        <table class="table table-bordered">
        {% for team in league %}
            <tr>
                <td>{{team}}</td>
                {% for data in league[team]|dictsort(league[team]['totalpts']) %}
                <td>{{ league[team]['totalpts'] }}</td>
                {% endfor %}
            </tr>
        {% endfor %}
    </table>
Run Code Online (Sandbox Code Playgroud)

通过它在这种情况下不排序任何东西...只需打印表中的值,无需任何订单...

有人可以帮帮我吗?

谢谢

Anu*_*pta 18

你这样做的方式是行不通的,因为一旦你使用{% for team in league %},你就已经在使用未排序的dict并从中提取键值对.

我认为|dictsort在这种情况下可能无法帮助你,因为你不能按键或值排序,而是按'totalpts'的值(sub-dict)值排序.

相反,您应该在将此字典传递给模板之前对其进行排序,方法如下:

>>> from collections import OrderedDict
>>> league={'PEDD United': {'id': 37828, 'rank': 12, 'totalpts': 307},'Fc Mbonabushia': {'id': 205633, 'rank': 6, 'totalpts': 356},'FC Slurp': {'id': 933573, 'rank': 11, 'totalpts': 312},'KFC_Overijse': {'id': 38861, 'rank': 5, 'totalpts': 362},'Fc Paris': {'id': 1538051, 'rank': 2, 'totalpts': 396}}
>>> league = OrderedDict(sorted(league.items(), key= lambda x: x[1]['totalpts'], reverse=True))
>>> print league
OrderedDict([('Fc Paris', {'id': 1538051, 'rank': 2, 'totalpts': 396}), ('KFC_Overijse', {'id': 38861, 'rank': 5, 'totalpts': 362}), ('Fc Mbonabushia', {'id': 205633, 'rank': 6, 'totalpts': 356}), ('FC Slurp', {'id': 933573, 'rank': 11, 'totalpts': 312}), ('PEDD United', {'id': 37828, 'rank': 12, 'totalpts': 307})])
Run Code Online (Sandbox Code Playgroud)

要对dict进行排序,我们将其转换为使用的(key,value)元组列表.items().假设x是一个这样的元组,x [1]包含带有'totalpts'键的字典.

>>> league.items()[0]
('Fc Paris', {'id': 1538051, 'rank': 2, 'totalpts': 396})  # = x
Run Code Online (Sandbox Code Playgroud)

所以现在我们x[1]['totalpts']使用reverse=True递减顺序对元组进行排序.

dict本身无法排序,它是一种无序的数据类型 - 你可以使用一个OrderedDict,或者你可以简单地使用元组:

>>> sorted(league.items(), key= lambda x: x[1]['totalpts'], reverse=True)
[('Fc Paris', {'id': 1538051, 'rank': 2, 'totalpts': 396}), ('KFC_Overijse', {'id': 38861, 'rank': 5, 'totalpts': 362}), ('Fc Mbonabushia', {'id': 205633, 'rank': 6, 'totalpts': 356}), ('FC Slurp', {'id': 933573, 'rank': 11, 'totalpts': 312}), ('PEDD United', {'id': 37828, 'rank': 12, 'totalpts': 307})]
Run Code Online (Sandbox Code Playgroud)


dpr*_*dpr 11

您可以使用普通sort过滤器对 dict 的项目进行排序。

要按键排序,请使用attribute=0

{% for key, value in data.items()|sort(attribute='0') %}
  {{ key }}: {{ value }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

按值排序 attribute=1

{% for key, value in data.items()|sort(attribute='1') %}
  {{ key }}: {{ value }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

要按值的属性排序,请使用 attribute=1.name

{% for key, value in data.items()|sort(attribute='1.name') %}
  {{ key }}: {{ value }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

那是问题中的循环将转化为

{% for team_name, team_attrs in league.items()|sort(attribute='1.totalpts') %}
  <td>{{ team_attrs.totalpts }}</td>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)