Home > Back-end >  How to add numbers i.e. increment before a sentence in python
How to add numbers i.e. increment before a sentence in python

Time:01-09

How do i add numbers before the sentences in 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 :

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

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?

CodePudding user response:

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='.) ')

CodePudding user response:

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:

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?

CodePudding user response:

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)

CodePudding user response:

You can use enumerate to increment automatically a counter and a f-string:

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
  •  Tags:  
  • Related