[英]Jinja2 unzip list of tuples
How can I unzip a list of tuples? 如何解压缩元组列表? I have got
我有
x = [(1,'a'), (2, 'b')....]
And want to have 想要拥有
x1 = [1,2..]
x2 = ['a', 'b'...]
I have set a zip filter in my flask app but I am not sure is it a proper approach: 我在烧瓶应用程序中设置了一个zip过滤器,但是我不确定这是否是正确的方法:
app.jinja_env.filters['zip'] = zip
And in template 并在模板中
{% set x1, x2 = *x|zip %}
But there is also no * feature in jinja. 但是Jinja中也没有*功能。 How to solve that?
怎么解决呢? Thanks
谢谢
You could define a function that star unpacks its input into zip
: 您可以定义一个将星标将其输入解压缩为
zip
的函数:
def unpack_and_zip(x):
return zip(*x)
Then register that as your filter 然后将其注册为您的过滤器
app.jinja_env.filters['unpack_and_zip'] = unpack_and_zip
{% set x1, x2 = x|unpack_and_zip %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.