Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 在jinja中对列表中的值进行四舍五入_Sorting_Flask_Jinja2_Rounding - Fatal编程技术网

Sorting 在jinja中对列表中的值进行四舍五入

Sorting 在jinja中对列表中的值进行四舍五入,sorting,flask,jinja2,rounding,Sorting,Flask,Jinja2,Rounding,我有一本字典 dictionary_num = {'Age': [-1.0, 25.0], 'M2': [-1.0, 42.0], 'younChild': [-1.0, 7.0], 'M4': [-1.0, 216.0]} 还有另一本字典 dictionary_mcqsr = {'M5': [-1.0, 3.0, 0.0, 2.0, 1.0], 'M6': [-1.0, 0.0, 1.0], 'M7': [-1.0, 1.0, 0.0], 'M8A': [0.0, -1.0, 1.0, 2.

我有一本字典

dictionary_num = {'Age': [-1.0, 25.0], 'M2': [-1.0, 42.0], 'younChild': [-1.0, 7.0], 'M4': [-1.0, 216.0]}
还有另一本字典

dictionary_mcqsr = {'M5': [-1.0, 3.0, 0.0, 2.0, 1.0], 'M6': [-1.0, 0.0, 1.0], 'M7': [-1.0, 1.0, 0.0], 'M8A': [0.0, -1.0, 1.0, 2.0], 'M8B': [1.0, 0.0, -1.0], 'M9': [0, 1], 'M11': [0, 1], 'M12': [2, 1, 0, -1]}
它被渲染为

{% for key,value in dictionary_num.items() %}
                        {% if key == j %}
                            <td>{{value}}</td>
                        {% endif %}
              {% endfor %}
{% for key,value in dictionary_mcqsr.items() %}
                            {% if key == j %}
                                <td>{{value}}</td>
                            {% endif %}
                  {% endfor %}
{%表示键,字典中的值。\u num.items()%}
{%if key==j%}
{{value}}
{%endif%}
{%endfor%}
{%表示键,字典中的值\u mcqsr.items()%}
{%if key==j%}
{{value}}
{%endif%}
{%endfor%}

其中j指钥匙。渲染时,我希望删除dictionary_num和dictionary_mcqsr中jinja端的小数点,并对dictionary_mcqsr的值进行排序,但找不到任何解决方案。

使用一个过滤器来过滤要舍入的内容:

示例:(对于单个元素)

编辑:对于列表:

{% for key,value in dictionary_num.items() %}
                        {% if key == j %}
                            <td>[{% for element in value %} {{ (element.value)|round|int }}{% if not loop.last %},{% endif %} {% endfor %}]</td>
                        {% endif %}
              {% endfor %}
{% for key,value in dictionary_mcqsr.items() %}
                            {% if key == j %}
                                <td>[{% for element in value %} {{ (element.value)|round|int }}{% if not loop.last %},{% endif %} {% endfor %}]</td>
                            {% endif %}
                  {% endfor %}
{%表示键,字典中的值。\u num.items()%}
{%if key==j%}
[{%for value%}{{(element.value)| round | int}{%if not loop.last%},{%endif%}{%endfor%}]
{%endif%}
{%endfor%}
{%表示键,字典中的值\u mcqsr.items()%}
{%if key==j%}
[{%for value%}{{(element.value)| round | int}{%if not loop.last%},{%endif%}{%endfor%}]
{%endif%}
{%endfor%}
您可以在Jinja中使用和
round
过滤器

{% for key, value in dictionary_num.items() %}
    {% if key == j%}
        <td>
        [
            {% for element in value %}
                {{ element.value | round | int }}
                {% if not loop.last%}
                    ,
                {% endif %}
            {% endfor %}
        ]
    {% endif %}
{% endfor %}
{% for key, value in dictionary_mcqsr.items() %}
    {% if key == j%}
        <td>
        [
            {% for element in value | sort %}
                {{ element.value | round | int }}
                {% if not loop.last%}
                    ,
                {% endif %}
            {% endfor %}
        ]
    {% endif %}
{% endfor %}
{%表示键,字典中的值。\u num.items()%}
{%if key==j%}
[
{值%%中元素的百分比}
{{element.value | round | int}
{%if not loop.last%}
,
{%endif%}
{%endfor%}
]
{%endif%}
{%endfor%}
{%表示键,字典中的值\u mcqsr.items()%}
{%if key==j%}
[
{值中元素的百分比|排序%}
{{element.value | round | int}
{%if not loop.last%}
,
{%endif%}
{%endfor%}
]
{%endif%}
{%endfor%}

是一个列表。因此,它给了我一个错误
TypeError:type list没有在顶级模板代码{%extends'layouts/main.html%%{%block title%}代码表{%endblock%}{%block content%}文件的第1行定义{code>文件“C:\kuhuconfig\templates\pages\placeholder.results.html”“C:\kuhuconfig\templates\layouts\main.html”,在顶级模板代码{%endfor%}{%endif%}{%endwith%}{%block content%}{%endblock%}文件C:\kuhuconfig\templates\pages\placeholder.results.html中的第122行,在块“content”{(element.value)| round | int}中的第60行TypeError:type Undefined没有定义\uuuuuu round\uuuuuu方法
它会引发此错误。顶级模板代码{%extends'layouts/main.html%%{%block title%}代码表{%endblock%}{%block content%}文件C:\kuhuconfig\templates\layouts\main.html中的第1行“,第122行,在顶级模板代码{%endfor%}{%endif%}{%endwith%}{%block content%}{%endblock%}文件“C:\kuhuconfig\templates\pages\placeholder.results.html”中,第60行,在块“content”{(element.value)| round | int}类型错误:未定义的类型没有定义round方法,它抛出此错误。”。
{% for key, value in dictionary_num.items() %}
    {% if key == j%}
        <td>
        [
            {% for element in value %}
                {{ element.value | round | int }}
                {% if not loop.last%}
                    ,
                {% endif %}
            {% endfor %}
        ]
    {% endif %}
{% endfor %}
{% for key, value in dictionary_mcqsr.items() %}
    {% if key == j%}
        <td>
        [
            {% for element in value | sort %}
                {{ element.value | round | int }}
                {% if not loop.last%}
                    ,
                {% endif %}
            {% endfor %}
        ]
    {% endif %}
{% endfor %}