您可以使用子类化列表的自定义类.这样,您可以调整.remove的行为:
class Lists(list):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# logic here, for now using the whole passed list
self.sides = self[:]
self.center = self[:]
def remove(self, obj):
# TODO catch ValueError that is raised if obj isn't in all of the lists
super().remove(obj)
self.sides.remove(obj)
self.center.remove(obj)
# probably overriding other methods from list, such as append, so
# an instance can be used directly to interact with the "master" list
my_lists = Lists(["first", "last", "middle", "top", "bottom", "left", "right", "inside"])
print(my_lists)
my_lists.remove('last')
print(my_lists)
print(my_lists.sides)
print(my_lists.center)
# ['first', 'last', 'middle', 'top', 'bottom', 'left', 'right', 'inside']
# ['first', 'middle', 'top', 'bottom', 'left', 'right', 'inside']
# ['first', 'middle', 'top', 'bottom', 'left', 'right', 'inside']
# ['first', 'middle', 'top', 'bottom', 'left', 'right', 'inside']
如果需要,您还可以更好地封装.master(如代码中的注释所示).
但是,您可能需要重新考虑问题以及您选择的解决问题的方法.可能有比保留原始列表的子列表更好的方法,并且还应记住,如果尝试从列表中删除不存在的元素,.remove将引发异常.