[英]What is the best way in python to assign each of the same element of one list to the same indices on another list?
I thought about having a word as a string, making it into a "regularList" of strings, generating a "dummyList" which contains a string '-'for each letter in the word , then looping through the "regularList", simultaneously removing each instance of my guessed letter from "regularList" and reassigning it to the same index of "dummyList". 我考虑具有字作为一个字符串,使得它变成一个字符串“regularList”,产生一“dummyList”,其中包含一个字符串“-'for在单词的每个字母,然后通过‘regularList’循环,同时除去各我从“ regularList”猜到的字母的实例,并将其重新分配给“ dummyList”的相同索引。 Basically, I need to make: 基本上,我需要做:
regularList = [['a', 'a', 'r', 'd', 'v', 'a', 'r', 'k']]
dummyList = ['_','_','_','_','_','_','_']
Into: 成:
regularList = [['r', 'd', 'v', 'r', 'k']]
dummyList = ['a','a','_','_','_','a','_','_']
Here is my attempt: 这是我的尝试:
word = 'aardvark'
def changeLetter(word):
guess = raw_input('Guess a letter:') # When called, guess:a
print word
dummyList = []
for i in word:
dummyList.append('_ ')
print dummyList
regularList = [list(i) for i in word.split('\n')]
print regularList
numIters = 0
while guess in regularList[0]:
numIters += 1
index = regularList[0].index(guess)
dummyList[index] = guess
del regularList[0][index]
print regularList
print dummyList
print numIters
changeLetter(word)
This code produces: 此代码产生:
Samuels-MacBook:python amonette$ python gametest.py
Guess a letter:a
aardvark
['_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ']
[['a', 'a', 'r', 'd', 'v', 'a', 'r', 'k']]
[['r', 'd', 'v', 'r', 'k']]
['a', '_ ', '_ ', 'a', '_ ', '_ ', '_ ', '_ ']
3
As you can see, the proper indices aren't being reassigned. 如您所见,没有重新分配适当的索引。
word = 'aardvark'
def changeLetter(word):
guess = raw_input('Guess a letter:') # When called, guess:a
print word
dummyList = []
for i in word:
dummyList.append('_ ')
print dummyList
regularList = [list(i) for i in word.split('\n')]
print regularList
numIters = 0
position = 0
length = len(regularList[0])
while numIters < len(regularList[0]):
if regularList[0][numIters] == guess:
dummyList[position] = guess
del regularList[0][numIters]
numIters -=1
position +=1
numIters +=1
print regularList
print dummyList
print numIters
changeLetter(word)
Your program has one mistake when you delete an element the size of array becomes small and the element which should be next becomes previous. 删除元素时,程序会犯一个错误,即数组的大小变小,而下一个要变的元素将成为前一个。
regularList[0] = ['a', 'a', 'r', 'd', 'v', 'a', 'r', 'k'] RegularList [0] = ['a','a','r','d','v','a','r','k']
while guess in regularList[0]:
In this loop, when you remove first a , list becomes ['a', 'r', 'd', 'v', 'a', 'r', 'k'] 在此循环中,当您首先删除a时,列表将变为['a','r','d','v','a','r','k']
Now when your loop continues, guess becomes 'r' that is next element in previous list. 现在,当您的循环继续进行时,猜测将变为“ r”,即上一个列表中的下一个元素。 Hence a which was previously at position 1 is neglected (0 based indexing). 因此,先前位于位置1的a被忽略(基于0的索引)。
Maybe with enumerate and then remove the elements: 也许用枚举然后删除元素:
for index, element in enumerate(regularList):
if element == "a":
dummyList[index] = element
regularList.remove("a")
The accepted answer is way too long, you can just use a clean list comprehension 接受的答案太长了,您可以使用一个整洁的列表理解
currentWord = [letter if letter in guessedLetters else "_" for letter in solution]
A fully functioning guess the word program without win logic would be 6 lines 一个没有猜测逻辑的全功能单词程序将是6行
solution = "aardvark"
guessedLetters = []
while True:
guessedLetters.append(input("Guess a letter "))
currentWord = [letter if letter in guessedLetters else "_" for letter in solution]
print(" ".join(currentWord))
This outputs: 输出:
Guess a letter a
a a _ _ _ a _ _
Guess a letter k
a a _ _ _ a _ k
Guess a letter v
a a _ _ v a _ k
Guess a letter r
a a r _ v a r k
Guess a letter d
a a r d v a r k
Guess a letter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.