Python Forum
Replace String with increasing numer [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace String with increasing numer [SOLVED]
#11
(Aug-06-2021, 11:01 PM)Yoriz Wrote: Call it with next as I did in the example above.

next also isn't a callable or a function I can pass around. I'd need to wrap it in something to hand it to re.sub().

If I hand sub a replacement string, then I have to call sub n times for n replacements. But if I hand sub a function, then it goes off and does all the work (in C code) and just calls the function for the replacement when necessary. That second thing is what I was doing for performance. But the argument there must be a true callable that will be handed a match object and must return a string that will become the replacement.
Reply
#12
from itertools import count
import re

target = "example"
counter = count(1)

paragraph = (
    "This example is an example on how to make an example with the"
    " word example. the example ends here with an example. for example"
)


paragraph = re.sub(re.escape(target), lambda _: f"{next(counter)}", paragraph)

print(paragraph)
Output:
This 1 is an 2 on how to make an 3 with the word 4. the 5 ends here with an 6. for 7
Reply
#13
Yes, exactly. I was trying for two minutes to wrap it through some method other than a lambda and just gave up. I think I had talked myself into thinking (but not testing) that the lambda wouldn't retain the state and would regenerate the iterator.
Reply
#14
Iteration of ideas already provided (can't be blamed as efficient but easy to write :-):

from itertools import count

paragraph = (
    "This example is an example on how to make an example with the"
    " word example. the example ends here with an example. for example")

counter = count(1)

while 'example' in paragraph:
    paragraph = paragraph.replace('example', f"{next(counter)}", 1)

print(paragraph)
Output:
This 1 is an 2 on how to make an 3 with the word 4. the 5 ends here with an 6. for 7
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 778 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace string in a nested Dictianory. SpongeB0B 2 1,220 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,608 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,536 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,125 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Find and Replace numbers in String giddyhead 2 1,248 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,265 Jul-11-2022, 09:22 AM
Last Post: ibreeden
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,238 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,168 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,286 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020