如何在Jinja2的索引处将元素插入列表

时间:2020-03-13 05:56:49

标签: flask jinja2

我已将变量中的分割字符串列表设置为:

test = a new version of app

{% set new_index = test.split(' ') %}

我正在尝试在“ new”之后插入“ easy”,如下所示:

{% set new_index = test.split(' ').insert(2,' easy') %}

然后{{ new_index }}返回None

还尝试了以下新变量:

{% set test1 = new_index.insert(2,' easy') %}

这也返回了相同的None

我阅读了文档,其中示例中也从未使用过insert方法

有没有一种方法可以实现,TIA会提供任何帮助

1 个答案:

答案 0 :(得分:1)

test = "a new version of app"
new_index = test.split(' ').insert(2,'easy')
print(new_index)

输出

None

尝试

test = "a new version of app"
new_index = test.split(' ')
new_index.insert(2,'easy')
print(new_index)

输出

['a', 'new', 'easy', 'version', 'of', 'app']

然后尝试使用此代码作为您的jinja2代码

{% set new_index = test.split(' ') %}
{% set another_new_index = new_index.insert(2,' easy') %}

然后{{ new_index }}将返回所需的输出

相关问题