Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中显示或隐藏列表中的项目_Python_List - Fatal编程技术网

在Python中显示或隐藏列表中的项目

在Python中显示或隐藏列表中的项目,python,list,Python,List,Im基本上需要能够显示或隐藏列表中的项目 这样,当我选择一个选项时,一个项目显示出来,如果它是隐藏的,如下图所示 a = ['A','B','C','D','E','F','G','H','I'] def askChoice(): choice = 0 if choice == 1: a[-1] = X ##Therefore last item in the list is hidden elif choice == 2: a[-1]

Im基本上需要能够显示或隐藏列表中的项目

这样,当我选择一个选项时,一个项目显示出来,如果它是隐藏的,如下图所示

a = ['A','B','C','D','E','F','G','H','I']

def askChoice():
    choice = 0
    if choice == 1:
        a[-1] = X ##Therefore last item in the list is hidden

    elif choice == 2:
        a[-1] = a[-1] ##Therefore item shown

    else:
        a[-1] = [] ## There an empty placeholder where any other item can be placed

    return choice

您需要存储有关列表中哪些项目显示或隐藏的信息

我会这样做:

a = [['A',True], ['B',True], ['C',True], ['D',True], ['E',True]]

def show(index):
    a[index][1] = True

def hide(index):
    a[index][1] = False

def display():
    print([x[0] for x in a if x[1]])

还有其他方法,但将信息存储在列表中意味着您不会遇到令人困惑的错误,因为您关于显示什么和不显示什么的数据与实际的可打印数据不匹配。它还可以确保您在更新列表时必须更新显示/隐藏数据,否则很容易被忽略。

您需要存储有关列表中哪些项目显示或隐藏的信息

我会这样做:

a = [['A',True], ['B',True], ['C',True], ['D',True], ['E',True]]

def show(index):
    a[index][1] = True

def hide(index):
    a[index][1] = False

def display():
    print([x[0] for x in a if x[1]])

还有其他方法,但将信息存储在列表中意味着您不会遇到令人困惑的错误,因为您关于显示什么和不显示什么的数据与实际的可打印数据不匹配。它还确保您在更新列表时必须更新显示/隐藏数据,否则很容易被忽略。

呃,什么?也许你可以详细说明一下这方面的内容,因为你似乎试图用一个毫无意义的列表来做一些事情<代码>a[-1]=a[-1]?这没有任何作用。将
choice
设置为0将确保每次执行最后一个条件,即
else
。@Amber,选项2中的[-1]应该显示最后一个项目,如果它已使用X隐藏。顺便说一句,我还不熟悉python,因此我需要您的帮助。谢谢上面没有显示我想要做什么,但我基本上需要显示或隐藏列表中的任何项目。我建议你花一点时间来学习Python教程或其他东西,只是为了基本掌握这门语言。呃,什么?也许你可以详细说明一下这方面的内容,因为你似乎试图用一个毫无意义的列表来做一些事情<代码>a[-1]=a[-1]?这没有任何作用。将
choice
设置为0将确保每次执行最后一个条件,即
else
。@Amber,选项2中的[-1]应该显示最后一个项目,如果它已使用X隐藏。顺便说一句,我还不熟悉python,因此我需要您的帮助。感谢上面的内容并没有确切地显示我想要做什么,但我基本上需要显示或隐藏列表中的任何项目。我建议大家花一点时间看看Python教程或其他东西,只是为了基本掌握该语言。