Home > Software engineering >  Add ascending numbers to words in a sentence
Add ascending numbers to words in a sentence

Time:07-18

Is it possible to add ascending numbers in order to words in a sentence with a formula?

For example:

enter image description here

CodePudding user response:

Try below formula-

=TEXTJOIN(" ",1,ArrayFormula(FLATTEN(SPLIT(A1,"/"))&SEQUENCE(LEN(A1)-LEN(SUBSTITUTE(A1,"/","")) 1)))

enter image description here

CodePudding user response:

Just use the enumerate function

Here is a short example

sentence = "He is playing the piano"  # I used spaces as seperators. It seems like you used slashes
new_sentence = []
for w, word in enumerate(sentence.split()):  # you can also change the seperators
    new_sentence.append(word str(w))

new_sentence = " ".join(new_sentence)
print(new_sentence)

Output: He0 is1 playing2 the3 piano4

  • Related