【问题标题】:Python: replace specific character in string (problem with duplicates)Python:替换字符串中的特定字符(重复问题)
【发布时间】:2020-10-12 08:55:48
【问题描述】:

我有一个程序的代码,它将字符串中的字符替换为上标字符,每次跳转 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 敏感?

【问题讨论】:

  • 你能说明一个带有空格或其他字符的单词应该如何映射吗?
  • @tobias_k 编辑了问题!
  • 谢谢,但是“并且下一个字符没有被替换”应该是“并且下一个字符现在被替换”吗?为什么你以不同的方式处理空格和“不是字母”?你能添加一个带空格的例子吗?

标签: python dictionary replace duplicates


【解决方案1】:

str.replace,不管有没有第三个参数,在这里都不是正确的选择,因为它总是从单词的开头开始替换。相反,如果所有条件都适用(在字典中,是偶数/奇数位置等),您可以一个一个地迭代字符并用字典中的对应字符替换它们。

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

我不太明白你想如何处理不在letters 中的空格和其他字符;在检查i % 2 == k 时,您可能需要计算跳过的字符数并考虑这些。

如果没有任何这样的“跳过”条件,您甚至可以将其变成单行:

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

【讨论】:

  • 枚举它们是个好主意,我真的应该为资源创建一个新的 str。谢谢,这很有帮助!
  • 这是最好的答案!最后一个班轮真的很简单。
  • @rage 接受最适合您的解决方案。这就是你可以说“谢谢”的方式。
  • 我猜“单行”部分吸引了我的注意力:)
【解决方案2】:

我在你的代码上做了几个星期,它的工作原理是这样的:

while True:
    text = input('Insert text: ')
    while True:
        fos = input('Do you want the first or the second letter to be small?(f/s): ')
        if fos in ['f','s']:
            break
        print('Please insert \'f\' or \'s\' (for first and second letters).\n')
    txt = ''
    if fos == 's':
        c = 'low'
    else:
        c = 'up'
    for l in text:
        if c == 'up' and l:
            txt += letters[l]
            c = 'low'
        else:
            txt += l
            c = 'up'
    print(txt)

测试:

Insert text: abaaba
Do you want the first or the second letter to be small?(f/s): f
ᵃbᵃaᵇa
Insert text: ababab
Do you want the first or the second letter to be small?(f/s): s
aᵇaᵇaᵇ
Insert text: 

【讨论】:

  • 哇,这真的很有帮助!非常感谢。
【解决方案3】:

只需简单地转换字符串中的所有其他字符 ...

for i in string(0, len(string), 2): #number 2 is equal to i

【讨论】:

    【解决方案4】:

    最好使用另一个字符串并将字符附加到它。

    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
       text2 = ''
       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
               text2+=e
               continue
    
           elif e == ' ': # Ignoring blank spaces
               text2+=e
               continue
    
           elif e not in letters: # Ignoring characters that are not in my dict
               text2+=e
               x = x + 1
               continue
    
           elif e in letters:
               text2 += letters[e]
               x = x + 1
    
       print(text2)
    

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      相关资源
      最近更新 更多