如何在python中的句子前添加數字。代碼如下
for output in outputs:
line = tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
print(line)
輸出是:
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?
我正在嘗試獲得類似的輸出
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?
誰能幫我這個?
uj5u.com熱心網友回復:
您可以使用enumerate自動遞增計數器和 f 字串:
outputs = ['line1', 'line2', 'line3']
for n, output in enumerate(outputs):
line = output.capitalize() # use your function here
print(f'{n 1}.) {line}')
輸出:
1.) Line1
2.) Line2
3.) Line3
uj5u.com熱心網友回復:
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='.) ')
uj5u.com熱心網友回復:
無需使用額外的變數。像這樣使用索引:
for index,output in outputs:
line = tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
print(str(index 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?
uj5u.com熱心網友回復:
您可以定義一個變數,在每個回圈中增加它并將數字添加到列印的文本中。
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)
uj5u.com熱心網友回復:
回傳一個列舉物件。iterable 必須是序列、迭代器或其他支持迭代的物件。
for i, output in enumerate(outputs, start=1):
line = output
print(i, '.)',line)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408653.html
標籤:
上一篇:SeleniumWebScraping:按文本查找元素在腳本中不起作用
下一篇:在這個for回圈中哪一行有錯誤?
