在Python中,当元素包含在变量中时,如何从列表中删除元素
我是编程新手,刚刚完成大学的第一学期课程。在处理课程的最后细节时,我遇到了一个挑战,我还没有找到关于堆栈溢出的答案 我有一个名为cL(colorslist)的列表,我试图从这个列表中删除一个元素,该元素包含在一个名为“color2”的变量中,该变量由用户通过shell使用“input”方法输入 我找到了使用list.remove(x)或list.pop(x)从列表中删除元素的方法,方法是引用元素本身(以字符串形式提供元素名称)或元素在列表中的索引位置 这就是我正在使用的代码的样子:在Python中,当元素包含在变量中时,如何从列表中删除元素,python,list,Python,List,我是编程新手,刚刚完成大学的第一学期课程。在处理课程的最后细节时,我遇到了一个挑战,我还没有找到关于堆栈溢出的答案 我有一个名为cL(colorslist)的列表,我试图从这个列表中删除一个元素,该元素包含在一个名为“color2”的变量中,该变量由用户通过shell使用“input”方法输入 我找到了使用list.remove(x)或list.pop(x)从列表中删除元素的方法,方法是引用元素本身(以字符串形式提供元素名称)或元素在列表中的索引位置 这就是我正在使用的代码的样子: #
# more code before this
cL = ["red", "green", "blue", "magenta", "orange", "pink"]
elif colour2 == colour1:
cL = cL.remove(colour2)
print("\nPlease select a different colour than previous one:\n", cL)
colour2 = str(input("Please re-enter second colour: "))
# more code to follow
基本上,我在这里试图实现的是,当用户在shell中输入提示他输入的第二种或第三种颜色时,如果他输入了他以前从列表中使用过的颜色,我试图从列表中删除该颜色,然后将新的修改列表呈现给用户,这样他就知道剩下哪些颜色可供选择
显然,我尝试的这段代码不起作用
cL = cL.remove(colour2)
我正试图看看在完成这项任务时有什么选择
多谢各位
*******包含完整代码的更新(适用于好奇的人)*******
*******任务在堆栈溢出社区的帮助下完成*******
cL = ["red", "green", "blue", "magenta", "orange", "pink"]
print("\nAvailable colours to choose from:\n", cL)
colour1 = str(input("Please enter FIRST colour: ")).lower()
while True:
if colour1 not in cL:
print("\nInvalid colour.\nPlease choose a colour from list:\n", cL)
colour1 = str(input("Please re-enter a valid FIRST colour: "))\
.lower()
else:
cL.remove(colour1)
break
print("\nGREAT WORK! Remaining valid colours to choose from are:\n", cL)
colour2 = str(input("Please enter SECOND colour: ")).lower()
while True:
if colour2 not in cL:
print("\nInvalid colour.\nPlease choose a colour from list:\n", cL)
colour2 = str(input("Please re-enter a valid SECOND colour: "))\
.lower()
else:
cL.remove(colour2)
break
这就是我完成的颜色输入验证代码的样子(第一种和第二种颜色)。我想这可能有助于将来完成类似任务的人
*颜色输入可以是[小写]、[大写]或[两者的混合],除非用户在选择任何颜色之前选择呈现给他的一种颜色,否则他将陷入一个循环,要求他从提供的列表中选择有效的颜色 另一种方法:
colour2 = str(input("Please re-enter second colour: "))
cL = [x for x in cL if x != colour2]
另一种方法是:
colour2 = str(input("Please re-enter second colour: "))
cL = [x for x in cL if x != colour2]
你就快到了
方法list.remove()修改列表,但不返回修改后的列表本身
因此,如果您执行以下操作:
cL.remove(colour2)
print(cL)
它会给你想要的回答。你就快到了
方法list.remove()修改列表,但不返回修改后的列表本身
因此,如果您执行以下操作:
cL.remove(colour2)
print(cL)
它应该会给您所需的响应。因为您不仅限于
color2
,而且希望多次这样做,所以我提供了一个更通用的解决方案,它将适用于任何数量的输入,除非您输入q
cL = ["red", "green", "blue", "magenta", "orange", "pink"]
deleted = []
while True:
colour = input("Please enter a colour to delete: (press q to quit)")
if colour == 'q':
break
if colour not in deleted:
cL.remove(colour)
deleted.append(colour)
else:
print ("%s already deleted. Try entering another color" %colour)
样本输出
Please enter a colour to delete: (press q to quit)red
Please enter a colour to delete: (press q to quit)red
red already deleted. Try entering another color
Please enter a colour to delete: (press q to quit)green
Please enter a colour to delete: (press q to quit)pink
Please enter a colour to delete: (press q to quit)red
red already deleted. Try entering another color
Please enter a colour to delete: (press q to quit)green
green already deleted. Try entering another color
Please enter a colour to delete: (press q to quit)q
由于您不仅限于
color2
,而且还想多次这样做,因此我提供了一个更通用的解决方案,它将适用于任何数量的输入,除非您输入q
cL = ["red", "green", "blue", "magenta", "orange", "pink"]
deleted = []
while True:
colour = input("Please enter a colour to delete: (press q to quit)")
if colour == 'q':
break
if colour not in deleted:
cL.remove(colour)
deleted.append(colour)
else:
print ("%s already deleted. Try entering another color" %colour)
样本输出
Please enter a colour to delete: (press q to quit)red
Please enter a colour to delete: (press q to quit)red
red already deleted. Try entering another color
Please enter a colour to delete: (press q to quit)green
Please enter a colour to delete: (press q to quit)pink
Please enter a colour to delete: (press q to quit)red
red already deleted. Try entering another color
Please enter a colour to delete: (press q to quit)green
green already deleted. Try entering another color
Please enter a colour to delete: (press q to quit)q
因此,据我所知,你有一个颜色列表,你希望用户开始从该列表中选择颜色,如果他选择了一种不在列表中的颜色,或者他已经选择的颜色,它应该告诉他选择一种不同的颜色 如果是这样的话,这就是它的样子
colors_list = ["red", "green", "blue", "magenta", "orange", "pink"]
colors_chosen = []
while colors_list:
print("Please select a color from this list: {}".format(colors_list))
color_selected = input()
while color_selected not in colors_list:
print("The color selected is not one of the options")
print("Please select a color from this list: {}".format(colors_list))
color_selected = input()
colors_list.remove(color_selected)
colors_chosen.append(color_selected)
print("These are the colors you chose: {}".format(colors_chosen))
因此,我们所做的是,我们有一个所有可能颜色的列表,并且我们使用一个循环,该循环将在该列表不为空时重复(如果您只希望用户选择n种颜色而不是所有颜色,则可以用for循环替换while循环)。在该循环中,我们将要求用户提供颜色,如果该颜色不在列表中,它将进入while循环,直到用户选择列表中的有效颜色。一旦用户选择了一种有效的颜色,我们将从列表中删除该颜色,并将其添加到一个列表中,我们将在其中存储用户选择的颜色。重复这个过程,直到颜色列表为空,就这样
如果用户键入的不是颜色的东西(如“car”或类似的东西),或者如果用户键入的是他/她以前已经选择的颜色,则可能需要显示不同的消息。为此,您需要检查用户键入的单词是否不在颜色列表中,但它是否在所选单词列表中,然后打印“您不能选择您已经选择的单词!”,如果它不在两个列表中,则打印“您必须选择实际颜色!”
希望有帮助 据我所知,您有一个颜色列表,您希望用户开始从该列表中选择颜色,如果他选择的颜色不在列表中,或者他已经选择的颜色,它应该告诉他选择其他颜色 如果是这样的话,这就是它的样子
colors_list = ["red", "green", "blue", "magenta", "orange", "pink"]
colors_chosen = []
while colors_list:
print("Please select a color from this list: {}".format(colors_list))
color_selected = input()
while color_selected not in colors_list:
print("The color selected is not one of the options")
print("Please select a color from this list: {}".format(colors_list))
color_selected = input()
colors_list.remove(color_selected)
colors_chosen.append(color_selected)
print("These are the colors you chose: {}".format(colors_chosen))
因此,我们所做的是,我们有一个所有可能颜色的列表,并且我们使用一个循环,该循环将在该列表不为空时重复(如果您只希望用户选择n种颜色而不是所有颜色,则可以用for循环替换while循环)。在该循环中,我们将要求用户提供颜色,如果该颜色不在列表中,它将进入while循环,直到用户选择列表中的有效颜色。一旦用户选择了一种有效的颜色,我们将从列表中删除该颜色,并将其添加到一个列表中,我们将在其中存储用户选择的颜色。重复这个过程,直到颜色列表为空,就这样
如果用户键入的不是颜色的东西(如“car”或类似的东西),或者如果用户键入的是他/她以前已经选择的颜色,则可能需要显示不同的消息。为此,您需要检查用户键入的单词是否不在颜色列表中,但它是否在所选单词列表中,然后打印“您不能选择您已经选择的单词!”,如果它不在两个列表中,则打印“您必须选择实际颜色!”
希望有帮助
cL=cL.remove(color2)
看起来可疑。我很确定remove不会返回列表。所以cL是那个val的colur列表