Python:替换字符串中的特定字符(重复的问题)
我有一个程序的代码,它将字符串中的字符替换为上标字符,每次跳1个字符 不在我的字典中的字符应该被跳过,但也会影响下一个字符是否被替换(因此,如果“不在dict中的字符”应该被替换,它将被跳过,下一个字符也不会被替换,反之亦然) 在不改变下一个字符的情况下,应跳过空格Python:替换字符串中的特定字符(重复的问题),python,dictionary,replace,duplicates,Python,Dictionary,Replace,Duplicates,我有一个程序的代码,它将字符串中的字符替换为上标字符,每次跳1个字符 不在我的字典中的字符应该被跳过,但也会影响下一个字符是否被替换(因此,如果“不在dict中的字符”应该被替换,它将被跳过,下一个字符也不会被替换,反之亦然) 在不改变下一个字符的情况下,应跳过空格 letters = { # My dictionary for all the letters and superscript versions 'a' : 'ᵃ', 'b' : 'ᵇ', 'c' : 'ᶜ',
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)
问题是,如果replace函数尝试替换的字符在字符串中有重复项,则它不关心其中哪个字符是“e”,而只替换字符串中的第一个字符
因此,如果用户输入'aba'和'f',结果将是'ᵃᵇᵃaba“应该是什么时候”ᵃBᵃA.ᵇa’。有没有办法使替换敏感于字符串中的哪个字符是e?
str。这里,无论是否使用第三个参数,替换都不是正确的选择,因为它总是从单词的开头开始替换。相反,如果所有条件都适用(在dict中,是偶数/奇数位置,等等),您可以逐个迭代字符,并用字典中的对应字符替换它们
我不太明白您希望如何处理非字母中的空格和其他字符
;在检查<代码> i % 2=k< /代码>时,您可能需要计算跳过的字符数,并考虑那些。
如果没有任何此类“跳过”条件,您甚至可以将其转换为一行:
res = ''.join(letters.get(c, c) if i % 2 == k else c for i, c in enumerate(text))
我对您的代码做了一些修改,其工作原理如下:
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)
测试:
只要简单地转换字符串中的其他字符
对于字符串中的i(0,len(string),2):#数字2等于i
最好使用另一个字符串并在其上附加字符
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)
你能展示一个带有空格或其他字符的单词应该如何映射吗?@tobias_k编辑了这个问题!谢谢,但是“应该”和下一个字符没有被替换“应该”和下一个字符现在被替换了“?为什么你用不同的方式处理空格和“非字母”?你可以添加一个空格的例子吗?哇,这真的很有帮助!非常感谢。列举它们是一个好主意,我真的应该为res制作一个新的str。。谢谢,这很有帮助!这是最好的答案!最后的一行非常简单。@rage接受最适合您的解决方案。这就是你说“谢谢”的方式。我想“一句话”的部分吸引了我的注意力:)
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:
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)