[英]How to add numbers i.e. increment before a sentence in python
How do i add numbers before the sentences in python.如何在 python 的句子前添加数字。 The code is given below
代码如下
for output in outputs:
line = tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
print(line)
And the output is: 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?
I'm trying to get the output like我正在尝试获得 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?
Can anyone help me with this?谁能帮我这个?
You can use enumerate
to increment automatically a counter and a f-string:您可以使用
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: 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='.) ')
No need to use extra variables.无需使用额外的变量。 Use index like this:
像这样使用索引:
for index,output in outputs:
line = tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
print(str(index+1) + '.) ' + line)
Output: 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?
you can define a variable, increase it on each loop and add the number to the printed text.您可以定义一个变量,在每个循环中增加它并将数字添加到打印的文本中。
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)
Return an enumerate object.返回一个枚举 object。 iterable must be a sequence, an iterator, or some other object which supports iteration.
iterable 必须是序列、迭代器或其他一些支持迭代的 object。
for i, output in enumerate(outputs, start=1):
line = output
print(i, '.)',line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.