|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class Liebiao:
- def __init__(self,*args):
- self.list1=[]
- for i in args:
- self.list1.append(i)
- self.dict1={}
- self.dict1.fromkeys(range(len(self.list1)),0)
- def __len__(self):
- return len(self.list1)
- def __getitem__(self,key):
- self.dict1[key]+=1
- return self.list1[key]
复制代码- >>> b=Liebiao(1,2,3,4,5,6)
- >>> b[1]
- Traceback (most recent call last):
- File "<pyshell#306>", line 1, in <module>
- b[1]
- File "C:/Users/ASUS/Desktop/嗨.py", line 11, in __getitem__
- self.dict1[key]+=1
- KeyError: 1
复制代码
诸位请问哪里出问题了呀
谢谢大家了
你的dict1是空的,是不是应该这样的?
- self.dict1=self.dict1.fromkeys(range(len(self.list1)),0)
复制代码
|
|