如何复制列表然后替换字符



我想复制我在python中的列表,然后更换字符。

例如:

如果代码要求您输入一个单词:(您输入('Hello''

'Hello'将被添加到列表中。

我想做的就是复制该列表,然后用" *****"替换" Hello"我希望"*"与" Hello"索引相同。

谢谢!

ps。我认为我的实际代码无关紧要,我只需要一个基本的理解。

编辑:

import getpass
hangman_pics = ['''
    +---+
        |
        |
        |
       ===''','''
    +---+
    O   |
        |
        |
       ===''','''
    +---+
    O   |
    |   |
        |
       ===''','''
    +---+
    O   |
   /|   |
        |
       ===''','''
    +---+
    O   |
   /|  |
        |
       ===''','''
    +---+
    O   |
   /|  |
   /    |
       ===''','''
    +---+
    O   |
   /|  |
   /   |
       ===''']
let_list = []
alreadyGuessed = []
correctGuessed = []
falseGuessed = []
lettersLeft = []
def player_input():
    word_input = getpass.getpass("Choose the word: ")
    for letter in word_input:
        let_list.append(letter)

def player_guess():
    while let_list:
        print ("Please enter your guess: ")
        guess = input()
        if guess in let_list:
            if guess in alreadyGuessed:
                print ('Oops, you already guessed ' + guess)
            else:
                alreadyGuessed.append(guess)
                correctGuessed.append(guess)
                print (guess + ' is correct!')
        elif guess in alreadyGuessed:
            print ('Oops, you already guessed ' + guess)
        else:
            alreadyGuessed.append(guess)
            falseGuessed.append(guess)
            print (guess + ' is wrong, sorry')
        while set(let_list) == set(correctGuessed):
            print ('Congratulations! You win!')
            return False
            break
        while len(falseGuessed) == len(hangman_pics):
            print ('Oh no, you killed him! You lose :(')
            return False
            break


player_input()
player_guess()

在python中使用 map函数;例如,

test = ['h', 'l', 'l', 'o']
def your_logic(element):
    if condition:
        return "*"
    return element
# every element in the list will be converted using your condition
result_list = map(your_logic, test)
list2 = []
for word in list1:
    if word == "hello":
        list2.append("*****")
    else:
        list2.append(word)

最新更新