简体   繁体   English

在python中,将一个列表的每个相同元素分配给另一列表的相同索引的最佳方法是什么?

[英]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.

相关问题 对于 Python 中另一个列表的每个不同值,计算一个列表的最大值和最小值的最佳方法是什么? - What is the best way to calculate maximum and minimum values for one list, for each distinct value of another list in Python? 有没有办法确定列表的元素是否与另一个列表(具有相同索引)的元素匹配? - Is there a way of determining if an element of a list matches the element of another list (of the same index)? 对列表中相同元素的索引进行分组的有效方法 - Efficient way to group indices of the same elements in a list 将数据从一个 python 脚本传输到另一个脚本而不在同一台计算机上的最佳方法是什么? - What would be the best way to transmit data from one python script to another, without them being on the same computer? 将一个列表替换为同一数组中python中的另一个列表 - replace one list with another list in python in the same array 如果一个列表中的数字与另一列表中的数字相同,则Python返回结果 - Python return a result if numbers in one list are the same in another list 为python中的列表/元组中的每个数据调用API的最佳方法是什么? - What is the best way to call API for each data in the list/tuple in python? 弹出一个元素并将相同元素附加到 Python 列表的时间复杂度是多少? - What is the time complexity of popping an element and appending the same element to a Python list? 如何在python中为列表的每个元素指定一个名称 - How to assign a name to each element of a list in python 将元素添加到 python 中的 dict 列表的最佳方法是什么? - what is the best way to add element to list of dict in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM