python - Django - 在多个值的两个列表中进行迭代,仅返回一组迭代
问题描述
我有一个与 zip() 和迭代有关的问题。
nutritional_info_quantity_per_user = []
if related_profiles:
for r in related_profiles:
r_perc = round((r.kcal/self.request.user.profile.kcaltotal)*100)
for intake in intakes:
intake_quantity = intake.amount
intake_custom_quantity_per_user = myround_two((intake_quantity/menu_kcal) * (kcal)*(r_perc/100))
nutritional_info_quantity_per_user.append(intake_custom_quantity_per_user)
(...)
nutritional_infos_per_user = zip(intakes, nutritional_info_quantity_per_user)
(...)
查询很好,因为它们返回完整的值列表。
但是当我在模板中循环遍历“nutritional_infos_per_user”时,如下所示:
{% for t, i in nutritional_infos_per_user %}
{{t.type.name}}
{{i}}
{% endfor%}
我只得到一次迭代(但是我有两个或更多“related_profiles”)。显然我做错了什么,如果有人能提供帮助,我将不胜感激!
非常感谢。
解决方案
推荐阅读
- r - 从 GLM 生成漂亮的表格
- python - 这个特定代码的空间和时间复杂度是多少
- neo4j - distance() 函数返回的单位是什么?即米/英尺/英寸/公里/英里等,在 Neo4j 中使用 geoCode 是否不准确
- flutter - 在flutter中将数据从MySQL数据库填充到DropDown
- javascript - 如何格式化悬停下拉菜单,使其始终位于顶层?
- php - 使用 php 和准备好的语句写入 mysql
- c++ - 如何对用 C++ 中的邻接表实现的图进行排序?
- java - Scala:由于链中的大量方法导致编译错误
- python - 我想检测用户的计算机语言,以便我可以根据它翻译页面
- python - 如何使用 Django 实现多种用户类型?