简体   繁体   English

如何在 python 中的句子前添加数字,即递增

[英]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.

相关问题 使用 Python 点击,如何添加超过 5 个选项,即超过 5 个? - Using Python Click, how do I add more than 5 options i.e. more than 5? 如何查看初始权重(即培训前)? - How to view initialized weights (i.e. before training)? Python 插件,即 spaCy 和 sciSpaCy - Python add ons i.e. spaCy and sciSpaCy Python:如何添加两个大的掩码数组并保持其值(即mask + value = value)? - Python: How to add two large masked arrays and keep their values (i.e. mask + value = value)? 如何链接多个参数? 即加(1)(2)(3)= 6 - How to chain multiple arguments? i.e. add(1)(2)(3) = 6 Python 正则表达式匹配数字但忽略其中包含数字的单词(即 N95 和 3D) - Python regex to match numbers but ignore words that have numbers in them (i.e. N95 and 3D) 如何做一个反“范围”,即根据一组数字创建一个紧凑的范围? - How to do an inverse `range`, i.e. create a compact range based on a set of numbers? 如何暗示数字*类型*(即数字的子类)——而不是数字本身? - How to hint at number *types* (i.e. subclasses of Number) - not numbers themselves? 如何从这种格式的数字列表中获取日期:14302610302017,即时间+日期 - how to get the date from the list of numbers of this format: 14302610302017, i.e., time + date 如何替换数据框列中的所有数字(附加字母/符号,即 43$)? - How to replace all numbers (with letters/symbols attached, i.e. 43$) in a dataframe column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM