简体   繁体   中英

Showing or hiding an item in a list in Python

Im basically need to be able to show or hide an item in a list

such that when i choose an option an item is shown if it's hidden such as below

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

You need to store the information about which items in the list are shown or hidden.

I would do something like:

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]])

There are other methods, but storing the info in your list means you won't run into confusing bugs where your data on what to show and what not to does not match up with your actual printable data. It also ensures you will have to update show/hide data when you update the list, which otherwise could be easily overlooked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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