繁体   English   中英

如何从列表中删除用户的输入?

[英]How do you remove a user's input from a list?

我正在处理此代码,但无法获取“ removeLetter”来删除用户从“选择”列表中选择的字母。 我发现我不能将列表函数与字符串一起使用,但是我不知道如何在不转换用户的字符串输入以匹配他/她想要的项目的情况下使start.remove()代码正常工作从“选择”列表中删除。 有人能帮助我吗?

import random

oneList = "a ", "b ", "c "
twoList = "d ", "e ", "f "
threeList = "g ", "h ", "i "
fourList = "j ", "k ", "l "

# Selects a letter at random from each list
oneRandom = random.choice(oneList)
twoRandom = random.choice(twoList)
threeRandom = random.choice(threeList)
fourRandom = random.choice(fourList)

# Displays chosen letter from each list
print("These are your letters.")
chosen = oneRandom + twoRandom + threeRandom + fourRandom
print(chosen)

# First user input
start  = input("Would you like to remove a letter? y or n? ")

# If start = yes then do this.
if start == 'y':
    removeLetter = input("What letter would you like to remove? ")

    # Removes user's chosen letter.
    keptLetters = chosen.remove(removeLetter)

    # Displays kept letters.
    print(keptLetters)

您的代码中有错误,特别是在此行中:

keptLetters = start.remove (removeLetter)

您正在调用变量startchosen的变量是带有字母列表的变量。 所以这应该工作:

keptLetters = chosen.remove (removeLetter)

您的代码中有几个问题。 我冒昧地为您提供了更多pythonic。

Python 2

import random

letter_sets = (("a", "b", "c"),
               ("d", "e", "f"),
               ("g", "h", "i"),
               ("j", "k", "l"))

# Selects a letter at random from each list
chosen = map(random.choice, letter_sets)

# Displays chosen letter from each list
print "These are your letters."
print " ".join(chosen)

# First user input
start = raw_input("Would you like to remove a letter? y or n?")

# If start = yes then do this.
if start[0].lower() == 'y':
    while(len(chosen) == 4): #Keep looping until a letter is chosen
        removeLetter = raw_input("What letter would you like to remove?")
        try:
            # Removes user's chosen letter.
            chosen.remove(removeLetter)
            # Displays kept letters.
            print " ".join(chosen)
        except ValueError: #If removeLetter is not in chosen
            print removeLetter, "is not in the list of letters"

Python 3

import random

letter_sets = (("a", "b", "c"),
               ("d", "e", "f"),
               ("g", "h", "i"),
               ("j", "k", "l"))

# Selects a letter at random from each list
chosen = list(map(random.choice, letter_sets))

# Displays chosen letter from each list
print("These are your letters.")
print(" ".join(chosen))

# First user input
start = input("Would you like to remove a letter? y or n?")

# If start = yes then do this.
if start[0].lower() == 'y':
    while(len(chosen) == 4): #Keep looping until a letter is chosen
        removeLetter = input("What letter would you like to remove?")
        try:
            # Removes user's chosen letter.
            chosen.remove(removeLetter)
            # Displays kept letters.
            print(" ".join(chosen))
        except ValueError: #If removeLetter is not in chosen
            print(removeLetter, "is not in the list of letters")
  1. 拥有多个列表,并重复执行代码不是很Python。 因此,我将“列表”作为元组的元组(因为您可能不打算对其进行修改)。 如果是这样,则通过用方括号[]而不是括号()表示列表来更改列表。
  2. 由于您要在多个列表上执行相同的操作,因此可以使用map()函数将所有结果都放入一个列表中。
  3. chosen应该保留一个列表,而不是字符串,因为将来您将通过删除字母来对其进行操作。
  4. 您可以轻松地join()所有在列表中的项目一起由单个空格分隔字符串" "
  5. 由于list.remove()可以在值不在列表中时发回ValueError ,因此您需要在try..except块中进行处理。
  6. 如果先前的尝试失败,可以用while循环包围您的逻辑,以不断要求删除一个字母。
  7. chosen.remove更改了原始数组,因此您无需将其保存到keptLetters

我希望这可以帮助你!

编辑:添加了等效的Python 2代码

Je Pense que sa doitêtreça

import random oneList = "a ", "b ", "c "
twoList = "d ", "e ", "f " 
threeList = "g ", "h ", "i " 
fourList = "j ", "k ", "l " 
# Selects a letter at random from each list 
oneRandom = random.choice(oneList) 
twoRandom = random.choice(twoList) 
threeRandom = random.choice(threeList) 
fourRandom = random.choice(fourList) 
# Displays chosen letter from each list 
print("These are your letters.") chosen = oneRandom + twoRandom + threeRandom + fourRandom print(chosen) 
# First user input 
start = input("Would you like to remove a letter? y or n? ") 
# If start = yes then do this. 
if start == 'y': removeLetter = input("What letter would you like to remove? ") 
# Removes user's chosen letter. 
keptLetters = chosen.remove(removeLetter)
# Displays kept letters. 
print(keptLetters)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM