You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Python中为输出的句子添加递增编号?

给输出句子添加递增数字编号的Python实现方法

嘿,这事儿其实超简单,只需要加个计数器跟踪编号就行!这里给你适配不同使用场景的实现方式:

场景1:批量处理多条输出(最常用)

如果你的output是包含多条结果的列表,用循环配合计数器就能完美解决:

# 初始化编号计数器,从1开始
count = 1

# 遍历所有输出结果(这里假设你的结果存在outputs列表里,按需替换成你的实际变量)
for output in outputs:
    line = tokenizer.decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
    # 把编号和句子拼接后打印
    print(f"{count}.) {line}")
    # 每次打印完,计数器自增1
    count += 1

场景2:单条输出的情况

如果只是偶尔输出单条内容,直接手动指定编号就好:

output = # 你的单个output结果
line = tokenizer.decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(f"1.) {line}")

运行后你就能得到完全符合期望的输出格式啦:

1.) I plan to visit Arsenal against Brighton match on April 9, and are you interested in meeting me at that match?
2.) Hey, I'm planning to visit the game Arsenal vs Brighton on 9th April, are you interested with me in watching this game?

内容的提问来源于stack exchange,提问作者Nithin Reddy

火山引擎 最新活动