输入:
class Item:
def __init__(self,ident):
self.ident = ident
G1 = {"1":Item("1"),"2":Item("2"),"3":Item("3")} #The whole group will be dead because Item_2 is dead
G2 = {"4":Item("4"),"5":Item("5"),} # item_4 will be dead because it's a successor of dead item (item_1)
G3 = {"6":Item("6"),"7":Item("7")} # still alive. Note that item 6 is predeceddor (and NOT successor of dead item)
G4 = {"8":Item("8"),"9":Item("8"),} # will be dead because item_4 is dead and item_8 is dead
allgroups = [G1,G2,G3,G4]
dead_items = {"2":Item("2")}
links = [(Item("1"),Item("2")), (Item("1"),Item("4")), (Item("6"),Item("1")), (Item("4"),Item("8"))]
规则:
目标是在过滤所有死掉的项目后,给出最终的组。在这个例子中,只有第三组仍然活着
我的基本尝试只考虑1:filteredallgroups = allgroups
for group in allgroups :
for item in group.values() :
print(item.ident)
if item.ident in dead_items.keys():
print("yes")
del group[item.ident]
此外,我不知道如何做2。有什么帮助吗
在执行上述循环之前,您是否考虑过使用另一个循环来处理死_项,对于后续项列表中的每个项,如果存在,则将后续项附加到死_项
然后,您可以立即运行循环所有的死项,无论是否继承,现在都在死项列表中
相关问题 更多 >
编程相关推荐