Luc*_*cas 2 python django dictionary
这是我的字典:
[{'entity': 'first entity', 'place': ['first', 'second', 'abc']}, {'entity': 'second entity', 'place': ['awe', 'ert']}]
Run Code Online (Sandbox Code Playgroud)
我想以这种方式打印值:
-first entity
-first, second, abc
-second entity
-awe, ert
Run Code Online (Sandbox Code Playgroud)
我尝试了很多东西,但我不知道如何处理第二个键的列表
你能否建议我如何在Django模板中做同样的事情?
提前致谢
对于python代码,
a = [{'entity': 'first entity', 'place': ['first', 'second', 'abc']}, {'entity': 'second entity', 'place': ['awe', 'ert']}]
for x in a:
print '-', x['entity']
print '-', ','.join(x['place'])
Run Code Online (Sandbox Code Playgroud)
对于Django模板:
<p>
{% for x in a %}
{{x.entity}} <br/>
{% for y in x.place %}
{{y}}
{% endfor %}
<br/>
{% endfor %}
</p>
Run Code Online (Sandbox Code Playgroud)
for d in my_list:
print "-%s\n-%s" % (d['entity'], ", ".join(d['place']))
Run Code Online (Sandbox Code Playgroud)
首先,请注意你所谓的"我的字典"实际上是一个字典列表,我在my_list这里打电话.这些词典中的每一个都有一个'entity'易于打印的键.该'place'键有一个值的列表.我用来.join()将该列表中的所有字符串与逗号空间字符串组合在一起,以生成您想要的人类可读列表.
| 归档时间: |
|
| 查看次数: |
7737 次 |
| 最近记录: |