首页 > 解决方案 > Python:替换字符串中的特定字符(重复问题)

问题描述

我有一个程序的代码,该程序将字符串中的字符替换为上标字符,每次跳转 1 个字符。

应该跳过不在我的字典中的字符,但也会影响是否替换下一个字符(因此,如果应该替换“not-in-dict-character”,则跳过它并且不替换下一个字符,反之亦然-反之亦然)

应该跳过空格而不改变下一个字符。

letters = { # My dictionary for all the letters and superscript versions
   'a' : 'ᵃ',
   'b' : 'ᵇ',
   'c' : 'ᶜ',
   'd' : 'ᵈ',
   'e' : 'ᵉ',
   'f' : 'ᶠ',
   'g' : 'ᵍ',
   'h' : 'ʰ',
   'i' : 'ᶦ',
   'j' : 'ʲ',
   'k' : 'ᵏ',
   'l' : 'ˡ',
   'm' : 'ᵐ',
   'n' : 'ⁿ',
   'o' : 'ᵒ',
   'p' : 'ᵖ',
   'q' : 'ᵠ',
   'r' : 'ʳ',
   's' : 'ˢ',
   't' : 'ᵗ',
   'u' : 'ᵘ',
   'v' : 'ᵛ',
   'w' : 'ʷ',
   'x' : 'ˣ',
   'y' : 'ʸ',
   'z' : 'ᶻ',
   'A' : 'ᴬ',
   'B' : 'ᴮ',
   'C' : 'ᶜ',
   'D' : 'ᴰ',
   'E' : 'ᴱ',
   'F' : 'ᶠ',
   'G' : 'ᴳ',
   'H' : 'ᴴ',
   'I' : 'ᴵ',
   'J' : 'ᴶ',
   'K' : 'ᴷ',
   'L' : 'ᴸ',
   'M' : 'ᴹ',
   'N' : 'ᴺ',
   'O' : 'ᴼ',
   'P' : 'ᴾ',
   'Q' : 'ᵠ',
   'R' : 'ᴿ',
   'S' : 'ˢ',
   'T' : 'ᵀ',
   'U' : 'ᵁ',
   'V' : 'ⱽ',
   'W' : 'ᵂ',
   'X' : 'ˣ',
   'Y' : 'ʸ',
   'Z' : 'ᶻ'
}

x = 0

while True:
   text = input('Insert text: ')

   while True:

   # This will ask if the user wants something like 'aᵃaᵃaᵃaᵃ' or 'ᵃaᵃaᵃaᵃa'

       fos = input('Do you want the first or the second letter to be small?(f/s): ')

       if fos != 'f':
           if fos != 's':
               print('Please insert \'f\' or \'s\' (for first and second letters).\n')
           else:
               break
       else:
           break

   if fos == 'f':
       x = 1
   elif fos == 's':
       x = 2

   for e in text:
       if x % 2 == 0: # If x value is even, it skips this character
           x = x + 1 # Makes the x value odd, so the next character isn't skipped
           continue

       elif e == ' ': # Ignoring blank spaces
           continue

       elif e not in letters: # Ignoring characters that are not in my dict
           x = x + 1
           continue

       elif e in letters:
           text = text.replace(e, letters[e], 1) # The third parameter is
           x = x + 1

   print(text)

问题是,如果替换函数试图替换的字符在字符串中有重复,它不关心哪个字符是'e',而只是替换字符串中的第一个字符。

因此,如果用户输入“abaaba”和“f”,结果将是“ᵃᵇᵃaba”,而应该是“ᵃbᵃaᵇa”。有没有办法让替换对字符串中的哪个字符是 e 敏感?

标签: pythondictionaryreplaceduplicates

解决方案


str.replace, with or without the third parameter, is not the right choice here, as it will always start replacing at the start of the word. Instead, you could just iterate the characters one by one and replace them with their counterpart from the dictionary if all the conditions apply (is in the dict, is even/odd position, etc.).

text = "Some Text"
k = 1
res = ""
for i, c in enumerate(text):
    if c in letters and i % 2 == k:
        res += letters[c]
    else:
        res += c

I did not quite understand how you want to handle spaces and other characters that are not in letters; you might have to count the number of skipped characters and consider those, too, when checking i % 2 == k.

Without any such "skip"-condition, you might even turn this into a one-liner:

res = ''.join(letters.get(c, c) if i % 2 == k else c for i, c in enumerate(text))

推荐阅读