Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 不可变字符串,typeerror:';str';对象不支持项分配_Python 3.x - Fatal编程技术网

Python 3.x 不可变字符串,typeerror:';str';对象不支持项分配

Python 3.x 不可变字符串,typeerror:';str';对象不支持项分配,python-3.x,Python 3.x,我正在使用其他人的代码来制作密码解算器,但它给了我 typeerror: 'str' object does not support item assignment" 在 有没有办法保持错误的相同含义?:) 因为key是str的类型 在Python中,字符串不支持像列表那样的项分配(正如错误消息明确指出的) 要将字符串的字符更新为给定索引,可以执行以下操作: string = "foobar" charindex = 2 # so you wants to replace the second

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

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

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


因为
key
str
的类型

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

因为
key
str
的类型

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

简单的回答是你有一个缩进错误。而不是:

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

更深层次的问题是,在原始代码中,在字符串和列表中都使用了
键。很明显,这很让人困惑,你最后的困惑就是明证!在我上面的版本中,我将它们分为两个不同的变量,我想你会发现更清楚。

简单的回答是你有一个缩进错误。而不是:

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

更深层次的问题是,在原始代码中,在字符串和列表中都使用了
键。很明显,这很让人困惑,你最后的困惑就是明证!在我上面的版本中,我将它们分为两个不同的变量,我想你会发现这两个变量更清楚。

在Python中,字符串是不可变的,所以你不能就地更改它们的字符。在Python中,字符串是不可变的,所以你不能就地更改它们的字符。
“”。只要
字符串[:charindex],join(…)
就太过分了+“O”+string[charindex+1::
非常确定字符串上的
+
是一种不好的做法(它直接绑定到C
+
,因此它不能与jython或类似的东西一起使用),这是。。。一点也不正确。C甚至没有字符串上的
+
,如果它这样做了,对PythonYes中
+
的实现仍然没有影响,我刚刚意识到我说的是错的,sry。但是我仍然记得
+
关于字符串不是一个好的实践(但是找不到任何源代码..)。今晚我们将继续搜索,如果需要,更新我的答案。无论如何,thx:)
'.join(…)
是一种过分的做法,因为你可以只使用
string[:charindex]+“O”+string[charindex+1://code>非常肯定
+
字符串是一种不好的做法(它直接绑定到C
+
,所以它不能与jython或类似的东西一起工作),这就是。。。一点也不正确。C甚至没有字符串上的
+
,如果它这样做了,对PythonYes中
+
的实现仍然没有影响,我刚刚意识到我说的是错的,sry。但是我仍然记得
+
关于字符串不是一个好的实践(但是找不到任何源代码..)。今晚我们将继续搜索,如果需要,更新我的答案。无论如何,thx:)