Home > database >  How to replace value in list based on word behind it and replace it by number generated from datafra
How to replace value in list based on word behind it and replace it by number generated from datafra

Time:06-10

I am trying to fix some of my generated sentence, by turning back some of the pre-processing i have done.

Generated_sentence = "Had to wait @ minutes for the pizza"

I want to change the @ sign back into a number. I have created a dataframe containing information about the median value and standard deviation of the numbers before the word minutes in the original dataset. I have also done this for some other word.

This is part of this dataframe

| word| median| std|
|---- |------| -----|
| minutes| 20| 20|
|pm|9|2|
|stars|3|2|

Now i want to change the @ back based on the word behind it so in this case that is minutes and then i want to generate a number based on the median value and the standard deviation. How do i do that?

CodePudding user response:

This should do what you're asking:

Generated_sentence = "Had to wait @ minutes for the pizza"
words = [x.strip() for x in Generated_sentence.split()]
word = words[words.index('@')   1]
sentence = Generated_sentence.replace('@', str(round(df.loc[df['word'] == word, 'median'].tolist()[0])))

Input

      word  median  std
0  minutes    20.5   20
1       pm     9.0    2
2    stars     3.0    2

Output

Had to wait 20 minutes for the pizza
  • Related