Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:08:41+00:00 2026-06-10T11:08:41+00:00

i think it over night,but couldn’t find a fast way to do it first

  • 0

i think it over night,but couldn’t find a fast way to do it

first came to place is replace()

for x,y in {'aa':'a','ab':'b'}.items():
    s = s.replace(x,y)

but it won’t work for this case:’aabcc’

correct is ‘abcc’
but output would be ‘bcc’

i can write some similar code like this (i have already write those code in various language like C#,php,actionscript,etc..)

   output = []
   def foo1(x):
      if(x!='a'):
          output.append(x)
      else:
          functionPointer = readafterA
   def readafterA(x):
      if(x=='a'):
          output.append('a')
      elif(x=='b'):
          output.append('b')
      else:
          output.append('a'+x)
      functionPointer = foo1
   functionPointer = normalExecute
   for singlechar in s:
      functionPointer(singlechar)
   print(''.join(output))

i don’t want write those code any more ,becuz:

1st,those code would messed up when the dictionary go big.

2nd,i believe there has to be some more elegant ways to do this job in Python

edit: i have fixed it in first paragraph,but that was not the point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:08:42+00:00Added an answer on June 10, 2026 at 11:08 am

    You can just not use a dict. Specify the tuples in the order you want to substitute them.

    >>> s = 'aabcc'
    >>> for x,y in (('ab','b'), ('aa','a')):
    ...     s = s.replace(x,y)
    ... 
    >>> s
    'abcc'
    

    You could also use regular expressions

    >>> s = 'aabcc'
    >>> import re
    >>> re.sub("a([ab])","\\1", s)
    'abcc'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I may be way in over my head here... I'm using a
I've read Visual VM remotely over ssh but I think I've not fully understood
jQuery selectors are wonderful, but I sometimes I find myself typing them over and
I think my previous question was over complicated and, to be honest, was confusing
When iterating over a standard container, do you think it's a good idea to
I got over a problem, I think a very specific one. I've got 2
I stumbled over a curious bug, I think: I tried to read 512 as
Okay, I think I need another pair of eyes to look over this. I'm
Help! I think i've gone in a bit over my head. I'm making a
I've counted, checked my commas, and went over the whole thing again but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.