如何从 Jinja 模板中存储为字符串的数字列表中找到最大值?

How to find max value from a list of numbers stored as string in a Jinja template?

我有一个存储在列表中的整数列表。显然它作为字符串存储在数据存储区中。所以我当前的整数列表是:

price = ['31412', '12300', '23000']

我想从列表中获取最大的数字。

{% for i in price %}
highest price is: {{ i | max }}
{% endfor %}

预期输出:

highest price is: 31412

我得到的是:

highest price is: 4

我怎样才能正确地做到这一点?

在找到最大值之前,您需要先将列表中的每个字符串转换为整数。

根据我对文档的理解,您应该能够像这样使用 map 过滤器:

highest price is: {{ price | map('int') | max }}

这应该等同于 Python 代码 max(map(int, price))

您不需要 for 结构。 max 过滤器已经隐式循环了 price 列表。