簡體   English   中英

如何在 python 中的句子前添加數字,即遞增

[英]How to add numbers i.e. increment before a sentence in python

如何在 python 的句子前添加數字。 代碼如下

for output in outputs:
    line = tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
    print(line)

output 是:

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

我正在嘗試獲得 output 之類的

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?

誰能幫我這個?

您可以使用enumerate自動增加一個計數器和一個 f 字符串:

outputs = ['line1', 'line2', 'line3']

for n, output in enumerate(outputs):
    line = output.capitalize() # use your function here
    print(f'{n+1}.) {line}')

output:

1.) Line1
2.) Line2
3.) Line3
for n_output,output in enumerate(outputs,start=1): line = tokenizer.decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=True) print(n_output, line, sep='.) ')

無需使用額外的變量。 像這樣使用索引:

for index,output in outputs:
    line = tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
    print(str(index+1) + '.) ' + line)

Output:

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?

您可以定義一個變量,在每個循環中增加它並將數字添加到打印的文本中。

n = 1
for output in outputs:
    line = f'{str(n).)} {tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)}'
    n += 1
    print(line)

返回一個枚舉 object。 iterable 必須是序列、迭代器或其他一些支持迭代的 object。

for i, output in enumerate(outputs, start=1):
    line = output
    print(i, '.)',line)

 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM