第一段代码继承了父类:
class CountList(list):
def __init__(self, *args):
super().__init__(args)
self.count = []
for i in args:
self.count.append(0)
def __len__(self):
return len(self.count)
def __getitem__(self, key):
self.count[key] += 1
return super().__getitem__(key)
def __setitem__(self, key, value):
self.count[key] += 1
super().__setitem__(key, value)
def __delitem__(self, key):
del self.count[key]
super().__delitem__(key)
def counter(self, key):
return self.count[key]
def append(self, value):
self.count.append(0)
super().append(value)
def pop(self, key=-1):
del self.count[key]
return super().pop(key)
def remove(self, value):
key = super().index(value)
del self.count[key]
super().remove(value)
def insert(self, key, value):
self.count.insert(key, 0)
super().insert(key, value)
def clear(self):
self.count.clear()
super().clear()
def reverse(self):
self.count.reverse()
super().reverse()
list1=CountList("one","two","there",5,4,3,"five","four")
第二段代码不继承父类:
class Mylist:
def __init__(self,*args):
self.values=[x for x in args]
value=[0 for x in args]
self.count=dict(zip(self.values,value))
def __len__(self):
return len(self.values)
def __getitem__(self,key):
self.count[self.values[key]]+=1
return self.values[key]
def __setitem__(self,key,value):
self.values[key]=value
def __delitem__(self,key):
self.count.pop(self.values[key])
self.values.remove(self.values[key])
def counter(self,index):
return self.count[self.values[index]]
def append(self,value):
self.values.append(value)
self.count[value]=0
def pop(self,index):
self.count.pop(self.values[index])
self.values.pop(index)
def remove(self,value):
self.count.pop(value)
self.values.remove(value)
def insert(self,index,value):
self.count[value]=0
self.values.insert(index,value)
def clear(self):
self.count.clear()
self.values.clear()
def reverse(self):
self.values.reverse()
list2=Mylist("one","two","there",5,4,3,"five","four")