【发布时间】:2017-06-07 01:29:47
【问题描述】:
我创建了一个基本上是一本爱好书的课程。可以通过两种方法访问这本书,enter(n,h) 取一个名字并不断向该名字添加爱好(一个名字可以有多个爱好)。另一种方法返回特定名称的一组爱好。我的爱好书将我插入到一个名字中的每一个爱好都存储起来。有人可以帮我修一下吗?
class Hobby:
def __init__(self):
self.dic={}
self.hby=set()
def enter(self,n,h):
if n not in self.dic.items():
self.dic[n]=self.hby
for k in self.dic.items():
self.hby.add(h)
def lookup(self,n):
return self.dic[n]
我尝试运行以下案例
d = Hobby(); d.enter('Roj', 'soccer'); d.lookup('Roj')
{'soccer'}
d.enter('Max', 'reading'); d.lookup('Max')
{'reading', 'soccer'} #should return just reading
d.enter('Roj', 'music'); d.lookup('Roj')
{'reading', 'soccer','music'} #should return soccer and music
【问题讨论】:
标签: python-3.x class dictionary set