【问题标题】:immutable strings, typeerror: 'str' object does not support item assignment不可变字符串,类型错误:“str”对象不支持项目分配
【发布时间】:2017-10-06 23:36:51
【问题描述】:

我正在使用其他人的代码来制作密码求解器,但它给了我

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

key[keyIndex] = cipherletter

有没有办法与错误保持相同的含义? :)

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)

【问题讨论】:

  • 在 Python 中,字符串是不可变的,所以你不能就地改变它们的字符。

标签: python-3.x


【解决方案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】:

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

    【讨论】:

    • ''.join(...) 是矫枉过正,当你可以string[:charindex] + "O" + string[charindex+1:]
    • 相当肯定+ 在字符串上是不好的做法(它直接绑定到 C +,因此它不适用于 jython 或类似的东西)
    • 那……根本不是真的。 C 甚至在字符串上都没有 +,如果有的话,这仍然与 Python 中 + 的实现无关
    • 是的,我刚刚意识到我说的是错误的,对不起。但我仍然记得字符串上的+ 不是一个好习惯(但找不到任何来源..)。好吧,今晚将继续搜索并在需要时更新我的​​答案。无论如何,谢谢:)
    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 2021-02-16
    • 2015-12-22
    • 1970-01-01
    • 2011-07-26
    • 2016-10-27
    • 2013-12-22
    相关资源
    最近更新 更多