You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python与Jinja2嵌套循环及if-else优化:实现分类下仅返回单个实例

Python逻辑调整:每个分类仅返回单个结果或默认文本

问题场景

现有Python代码会为每个分类遍历所有文件,导致每个分类下输出多条内容。需求是:对每个分类,仅输出一条内容——如果找到匹配的文件则输出该文件,否则输出Standard text block B。最终要在Jinja2中实现,先解决Python层面的逻辑。

原代码

categories = [[1],
              [2],
              [3],
              [4],
              [5],
              [6],
              [7],
              [8]]

files = [(5, 1, 25, 1, 'file.pdf'),
         (6, 1, 25, 3, 'file.pdf'),
         (7, 1, 25, 6, 'file.pdf'),]


for c in categories:
    print(f'Category: {c[0]}')
    for f in files:
        if c[0] == f[3]:
            print(f)
        else:
            print('Standard text block B')

当前输出

Category: 1
(5, 1, 25, 1, 'file.pdf')
Standard text block B
Standard text block B
Category: 2
Standard text block B
Standard text block B
Standard text block B
Category: 3
Standard text block B
(6, 1, 25, 3, 'file.pdf')
Standard text block B
Category: 4
Standard text block B
Standard text block B
Standard text block B
Category: 5
Standard text block B
Standard text block B
Standard text block B
Category: 6
Standard text block B
Standard text block B
(7, 1, 25, 6, 'file.pdf')
Category: 7
Standard text block B
Standard text block B
Standard text block B
Category: 8
Standard text block B
Standard text block B
Standard text block B

期望输出

Category: 1
(5, 1, 25, 1, 'file.pdf')
Category: 2
Standard text block B
Category: 3
(6, 1, 25, 3, 'file.pdf')
Category: 4
Standard text block B
Category: 5
Standard text block B
Category: 6
(7, 1, 25, 6, 'file.pdf')
Category: 7
Standard text block B
Category: 8
Standard text block B

Python解决方案

优化思路

  1. 优先将files转换为字典,用匹配键(元组第4个元素f[3])作为key,对应文件作为value,避免重复遍历文件,提升效率。
  2. 遍历分类时直接从字典查找匹配项,找不到则输出默认文本;也可以用循环查找+标记的方式,找到匹配项后立即终止遍历。

字典映射版代码

categories = [[1], [2], [3], [4], [5], [6], [7], [8]]
files = [(5, 1, 25, 1, 'file.pdf'), (6, 1, 25, 3, 'file.pdf'), (7, 1, 25, 6, 'file.pdf')]

# 构建文件映射字典:key为匹配的分类ID,value为对应的文件
file_map = {f[3]: f for f in files}

for c in categories:
    cat_id = c[0]
    print(f'Category: {cat_id}')
    # 查找匹配项,找不到则用默认文本
    print(file_map.get(cat_id, 'Standard text block B'))

循环查找版代码

categories = [[1], [2], [3], [4], [5], [6], [7], [8]]
files = [(5, 1, 25, 1, 'file.pdf'), (6, 1, 25, 3, 'file.pdf'), (7, 1, 25, 6, 'file.pdf')]

for c in categories:
    cat_id = c[0]
    print(f'Category: {cat_id}')
    matched = None
    # 遍历文件找匹配项,找到即停止
    for f in files:
        if f[3] == cat_id:
            matched = f
            break
    # 输出结果
    print(matched if matched is not None else 'Standard text block B')

Jinja2实现方案

基于字典映射的模板代码

将Python中构建的file_map字典传入模板,直接通过键查找:

{% for c in categories %}
Category: {{ c[0] }}
{{ file_map.get(c[0], 'Standard text block B') }}
{% endfor %}

基于循环查找的模板代码

无需提前构建字典,在模板内完成匹配查找:

{% for c in categories %}
Category: {{ c[0] }}
{% set matched = none %}
{% for f in files %}
    {% if f[3] == c[0] %}
        {% set matched = f %}
        {% break %}
    {% endif %}
{% endfor %}
{{ matched if matched else 'Standard text block B' }}
{% endfor %}

内容的提问来源于stack exchange,提问作者Enrique Bruzual

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 11:43:21