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

时间:2017-06-05 16:19:34

标签: python list

我希望复制我在python中的列表,然后替换字符。

例如:

如果代码要求您输入一个单词:(并且您输入)' hello'

'你好'将被添加到列表中。

我想要做的是复制该列表,然后替换“你好”#39;用' *****' 我想要' *'与'你好'相同的索引索引。

谢谢!

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

2 个答案:

答案 0 :(得分:0)

list2 = []
for word in list1:
    if word == "hello":
        list2.append("*****")
    else:
        list2.append(word)

答案 1 :(得分:0)

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