I have a number of lists that can be “opened” or “closed”, something like this:
lista = ["a", "b", "c"]
listb = ["d", "e"]
listc = ["a", "b", "e"]
listd = ["c", "d"]
I have a master list of all open items:
all_open = ["a", "b", "c", "e"]
and a list of open lists:
open_lists = ["lista", "listc"]
As the sublists are openend, their items are added to the master list:
open_lists.append("listb")
for each i in listb:
if !(i in all_open):
all_open.append(i)
Is there a simple algorithm to remove items from the master list when a sublist is closed? The goal is to not remove items that belong to other lists that are still open.
You have to keep track of how many lists each item was from. The simplest way to do that is with a map. I like to use
collections.Counterfor something like this.In addition, you can get rid of
all_openaltogether and use thecount.keys()iterator instead.