‘List’ 对象没有 ‘replace’ 属性

huangapple go评论103阅读模式
英文:

'List' object has no attribute 'replace'

问题

不要回答我要翻译的问题。以下是翻译好的部分:

我正在尝试将我的 first_guess 的索引位置填入 d 数组中,但我不确定在 replace 数组函数之前应该放什么。需要帮助哈哈

我创建了一个名为 f 的新变量,并在 replace 函数之前使用 d 数组作为列表变量。
我知道 first_guess 的位置,但不确定如何将其输入到 replace 函数中,以将 d 变量中的 '_' 替换为 first_guess。

英文:

I'm trying to fill in the index location of my first_guess into my d array but im unsure what goes before the replace array function. need help lol

CODE PIC

I created a new variable called f, and im using the d array as the list varible before the replace function..
I have the location of the first_guess but im unsure how to input that into the replace function to replace the '_' in the d variable with the first_guess.

答案1

得分: 1

replace方法适用于字符串,而不是列表。如果你需要使用replace,你可以将d转换为字符串,如 d = "".join(d),或者使用列表推导式 - f = list((y if i == first_guess else i) for i in d)

然而,根据你的描述,你并不需要使用replace。replace方法查找字符串的所有出现并将其替换为不同的字符串。你正试图将一个元素分配给一个索引,而不管它的当前值是什么。

你可以选择以下之一:

f = d[:first_guess] + [y] + d[first_guess + 1:]

这意味着你创建一个新列表,它包括从d的开头直到索引first_guess之前的所有元素,然后是y,然后是索引first_guess之后的所有元素。

或者

f = d.copy()
f[first_guess] = y

这意味着"在索引first_guess处将f分配为y"。在第二种解决方案中,你需要复制d,因为列表分配只是创建对列表的引用,而不是实际创建一个新列表。所以如果你不复制,改变f会改变d。

英文:

replace method is for strings, not lists. If you needed replace, you could convert d to a string using d = "".join(d), or use a list comprehension - f = list((y if i == first_guess else i) for i in d)

However you don't need replace for what you are describing. Replace finds all occurences of a string, and replaces it with a different string. You are trying to assign an element to an index, regardless of its current value.

You either need: f = d[:first_guess] + [y] + d[first_guess + 1:], meaning you create a new list that takes all elements from d until index first_guess, then y, and then all elements after index first_guess

or

f = d.copy()
f[first_guess] = y

Which means "f at index first_guess gets assigned y". The reason you need to copy d in the second solution is because list assignment just creates the reference to the list, it doesn't actually a create a new one. So if you didn't copy, changing f would change d.

答案2

得分: 0

替换方法适用于字符串而不是列表。此外,默认情况下,它替换字符的所有实例(在这种情况下为'_'),无法精确确定替换发生的索引位置。

解决方法是使用列表索引来重新分配值,以匹配隐藏单词中猜测的字母的索引位置。

以下是我提出的代码。我也是Python代码的新手,所以我相信有一种更简洁的代码块可以实现这一功能。我的代码考虑了重复猜测相同字母和不正确提示的情况,并提供了相应的“对策”。

hidden_word = input('输入一个单词')
x = len(hidden_word)
d = ['_'] * x
print(".".join(d))
responses = []
while d.count('_') != 0:
    accepted_inputs = ('y', 'n', 'yes', 'no')
    run = input("玩游戏吗? (Y = '是', N = '否')")
    if not run.lower() in accepted_inputs:
        print('错误的输入')
        continue
    if run.lower() == 'y':
        guess = input('猜一个字母')
        if guess in responses:
            print('已经猜过了,请尝试其他字母')
            continue
        else:
            responses.append(guess)
        for (n, m) in enumerate(hidden_word):
            if guess == m:
                d[n] = guess
        print(".".join(d))
    else:
        break
if d.count('_') == 0:
    print("恭喜你猜中了!")
英文:

The issue is that replace method is for strings and not lists. Plus it replaces all instances of a character ('_' in this case) by default and you can't pinpoint the index for which the replacement has to happen.

The solution is to use list indexing to reassign values at specific list index locations that match the index of the guessed letter in the hidden_word.

Below is the code that I have come up with. I'm also a newbie to python code, so I'm pretty sure that there will be a more concise code block for this. My code takes in the possibility of repeated guesses of the same letter and improper prompts and provides appropriate "counter measures" for the same.

Cheers!

hidden_word =input('Enter a word')
x = len(hidden_word)
d = ['_']*x
print(".".join(d))
responses = []
while d.count('_')!=0:
    accepted_inputs=('y','n','yes','no')
    run = input("Playing? (Y = 'Yes', N = 'No')")
    if not run.lower() in accepted_inputs:
        print('Wrong input')
        continue
    if run.lower() == 'y':
        guess = input('Take a Guess')
        if guess in responses:
                print('Already guessed, try something else')
                continue
        else:
            responses.append(guess)
        for (n,m) in enumerate(hidden_word):
            if (guess == m):
                d[n]=guess
        print(".".join(d))
    else:
        break
if d.count('_')==0:
    print("Congratulations you guessed it!")

huangapple
  • 本文由 发表于 2023年2月18日 18:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定