flask - How to insert an element into list at index in jinja2
问题描述
i have set the list of split string in a variable as:
test = a new version of app
{% set new_index = test.split(' ') %}
I am trying to insert 'easy' after 'new' so tried as:
{% set new_index = test.split(' ').insert(2,' easy') %}
then {{ new_index }}
which returned None
also tried with a new variable as:
{% set test1 = new_index.insert(2,' easy') %}
this also returned the same None
I read docs in which insert method is never used in examples too
Is there a way to achieve this, any help is appreciated TIA
解决方案
test = "a new version of app"
new_index = test.split(' ').insert(2,'easy')
print(new_index)
output
None
try This
test = "a new version of app"
new_index = test.split(' ')
new_index.insert(2,'easy')
print(new_index)
output
['a', 'new', 'easy', 'version', 'of', 'app']
Then Try this code for your jinja2 code
{% set new_index = test.split(' ') %}
{% set another_new_index = new_index.insert(2,' easy') %}
then {{ new_index }}
would return the required output
推荐阅读
- python-3.x - 类型错误:元组索引必须是整数或切片,而不是 str --python flask
- laravel - Laravel 雄辩的 whereHas 第一次约会在特定日期之后
- clojure - Bidi 发布请求被禁止但获取请求有效
- string - 如何在 FOR 循环中对 Python Pandas 列表中的元素执行字符串更改
- swift - ARKit - 沿特定方向调整对象大小
- youtube-api - YouTube 数据 API - 频道 ID 在 2 月 28 日一夜之间发生了变化
- excel - getElementBy(Value?) 需要检查元素是否被点击打开(true) 或关闭(false)
- javascript - 添加新项目时vsCode刷新树
- python - 有没有办法使用 .txt 作为输入?
- oracle-sqldeveloper - 在 Oracle SQL Developer 中遇到 GUI 延迟