Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to loop a list and unable to add it to the string in python

For example, I do have a code like below, where

res = get_prediction_eos(input_text)
print("res is : ", res)

answer = []

print("res ", res['bert'].split("\n"))

for i in res['bert'].split("\n"):
     answer.append(i)
answer_as_string = "    ".join(answer)

print("answer is : ", answer_as_string)

And the output for the above code is returned as below :

res is :  {'bert': 'up\nwrong\nhappening\nnew\nhappened'}
res  ['up', 'wrong', 'happening', 'new', 'happened']
answer is :  up    wrong    happening    new    happened

I do have a text like below code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

text = "Hey what's"

The main required output is ‘answer is : up wrong happening new happened’. I do have a text like ‘Hey what’s’ and i want to add the answer’s outputs to the text like

Hey what's up
Hey what's wrong
Hey what's happening
Hey what's new
Hey what's happened

Can anyone help me with the code?

>Solution :

You just need to iterate over the list and print text + the element in the loop.

In Python >= 3.6:

for e in res['bert'].split('\n'):
    print(f'{text} {e}?')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading