簡體   English   中英

Python:替換字符串中的特定字符(重復問題)

[英]Python: replace specific character in string (problem with duplicates)

我有一個程序的代碼,該程序將字符串中的字符替換為上標字符,每次跳轉 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)

問題是,如果替換 function 試圖替換的字符在字符串中具有重復項,則它不關心哪個字符是“e”,而只是替換字符串中的第一個字符。

因此,如果用戶輸入“abaaba”和“f”,結果將是“ᵃᵇᵃaba”,而應該是“ᵃbᵃaᵇa”。 有沒有辦法讓替換對字符串中的哪個字符是 e 敏感?

str.replace ,帶或不帶第三個參數,在這里不是正確的選擇,因為它總是會在單詞的開頭開始替換。 相反,如果所有條件都適用(在字典中,是偶數/奇數 position 等),您可以一個一個地迭代字符並用字典中的對應字符替換它們。

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))

我在你的代碼上做了幾個 tweeks,它的工作原理是這樣的:

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: 

最好使用另一個字符串和 append 字符。

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)

只需簡單地轉換字符串中的所有其他字符...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM