2017-10-06 6 views
0

をサポートしていない私は暗号ソルバーを作るために他の誰かのコードを使用していますが、それはあり不変の文字列、TypeError例外:「strの」オブジェクトは、アイテムの割り当て

key[keyIndex] = cipherletter 

でくれ

typeerror: 'str' object does not support item assignment" 

を与えますエラーと同じ意味を維持する方法? :)

def decryptWithCipherletterMapping(ciphertext, letterMapping): 
# Return a string of the ciphertext decrypted with the letter mapping, 
# with any ambiguous decrypted letters replaced with an _ underscore. 

# First create a simple sub key from the letterMapping mapping. 
    key = ['x'] * len(LETTERS) 
    for cipherletter in LETTERS: 
     if len(letterMapping[cipherletter]) == 1: 
# If there's only one letter, add it to the key. 
      keyIndex = LETTERS.find(letterMapping[cipherletter][0]) 
      key[keyIndex] = cipherletter 
     else: 
      ciphertext = ciphertext.replace(cipherletter.lower(), '_') 
      ciphertext = ciphertext.replace(cipherletter.upper(), '_') 
      key = ''.join(key) 

# With the key we've created, decrypt the ciphertext. 

return simpleSubCipher.decryptMessage(key, ciphertext) 
+0

Pythonでは、文字列は不変なので、文字をインプレースで変更することはできません。 –

答えて

1

keystrのタイプですので。

Pythonでは、文字列はリストのような項目の割り当てをサポートしていません(明示的にエラーメッセージが表示されるため)。

指定したインデックスにある文字列の文字を更新するには、次のような何かすることができます

string = "foobar" 
charindex = 2 # so you wants to replace the second "o" 
print(string[0:charindex] + 'O' + string[charindex+1:]) 
# gives foObar 

または関数にそれを回す:

def replace_at_index(string, replace, index): 
    return string[0:index] + replace + string[index+1:] 

print(replace_at_index('EggsandBacon', 'A', 4)) 
# gives EggsAndBacon 

ですから、同じようにそれを使用します。

key = replace_at_index(key, cipherletter, keyIndex) 
+0

'string ':charindex] +" O "+ string [charindex + 1:]' – trentcl

+0

文字列では ''' .join(...) 'は残念です。 C + +に直接バインドされているので、jythonやそのようなものでは機能しません) – Arount

+0

これは...まったく真実ではありません。 Cは文字列に '+'をつけていなくても、それがPythonの '+'の実装に何の影響も与えないならば、 – trentcl

1

簡単な答えは、インデントエラーがあることです。代わりに:

# First create a simple sub key from the letterMapping mapping. 
    key = ['x'] * len(LETTERS) 
    for cipherletter in LETTERS: 
     if len(letterMapping[cipherletter]) == 1: 
# If there's only one letter, add it to the key. 
      keyIndex = LETTERS.find(letterMapping[cipherletter][0]) 
      key[keyIndex] = cipherletter 
     else: 
      ciphertext = ciphertext.replace(cipherletter.lower(), '_') 
      ciphertext = ciphertext.replace(cipherletter.upper(), '_') 
      key = ''.join(key) 

あなたが持っている必要があります。

# First create a simple sub key from the letterMapping mapping. 
keyList = ['x'] * len(LETTERS) 

for cipherletter in LETTERS: 
    if len(letterMapping[cipherletter]) == 1: 
     # If there's only one letter, add it to the key. 
     keyIndex = LETTERS.find(letterMapping[cipherletter][0]) 
     keyList[keyIndex] = cipherletter 
    else: 
     ciphertext = ciphertext.replace(cipherletter.lower(), '_') 
     ciphertext = ciphertext.replace(cipherletter.upper(), '_') 

key = ''.join(keyList) 

より深い問題が両方文字列リストで、元のコードでは、あなたがkeyを使用していることです。明らかに、あなたが混乱してしまったという証拠として、それは混乱しています!上記の私のバージョンでは、これらを2つの異なる変数に分けました。私はあなたがより明確になると思います。

関連する問題