python使用类添加、连接和删除项

python使用类添加、连接和删除项,python,class,superclass,python-3.6,Python,Class,Superclass,Python 3.6,做这个作业,我不明白这是怎么回事,所以用步骤解释会有很大帮助。 这个问题很难理解,所以我们无法理解和尝试。 这是一个问题,分为三个部分。 您将获得以下超类。不要修改这个 class Container(object): """ Holds hashable objects. Objects may occur 0 or more times """ def __init__(self): """ Creates a new container with no ob

做这个作业,我不明白这是怎么回事,所以用步骤解释会有很大帮助。 这个问题很难理解,所以我们无法理解和尝试。 这是一个问题,分为三个部分。 您将获得以下超类。不要修改这个

class Container(object):
    """ Holds hashable objects. Objects may occur 0 or more times """
    def __init__(self):
        """ Creates a new container with no objects in it. I.e., any object 
            occurs 0 times in self. """
        self.vals = {}
    def insert(self, e):
        """ assumes e is hashable
            Increases the number times e occurs in self by 1. """
        try:
            self.vals[e] += 1
        except:
            self.vals[e] = 1
    def __str__(self):
        s = ""
        for i in sorted(self.vals.keys()):
            if self.vals[i] != 0:
                s += str(i)+":"+str(self.vals[i])+"\n"
        return s
编写一个实现以下规范的类。不要重写容器的任何方法

class Bag(Container):
    def remove(self, e):
        """ assumes e is hashable
            If e occurs one or more times in self, reduces the number of 
            times it occurs in self by 1. Otherwise does nothing. """
        # write code here
def count(self, e):
    """ assumes e is hashable
        Returns the number of times e occurs in self. """
    # write code here
class ASet(Container):
    def remove(self, e):
        """assumes e is hashable
           removes e from self"""
        # write code here
def is_in(self, e):
    """assumes e is hashable
       returns True if e has been inserted in self and
       not subsequently removed, and False otherwise."""
    # write code here
•例如,d1=行李()

•例如,d1=行李()

第二部分:

在袋子中写一个方法,如果b1和b2是袋子,那么b1+b2会给出一个新袋子,代表两个袋子的结合

•例如,a=行李()

第三部分:

编写一个实现以下规范的类。不要重写容器的任何方法

class Bag(Container):
    def remove(self, e):
        """ assumes e is hashable
            If e occurs one or more times in self, reduces the number of 
            times it occurs in self by 1. Otherwise does nothing. """
        # write code here
def count(self, e):
    """ assumes e is hashable
        Returns the number of times e occurs in self. """
    # write code here
class ASet(Container):
    def remove(self, e):
        """assumes e is hashable
           removes e from self"""
        # write code here
def is_in(self, e):
    """assumes e is hashable
       returns True if e has been inserted in self and
       not subsequently removed, and False otherwise."""
    # write code here
•例如,d1=ASet()


谢谢。

如果你想写一个子类,你需要做的第一件事就是理解你想做的子类。因此,您需要做的第一件事是了解
容器的功能

它有两种神奇的方法,
\uuuuu init\uuuuu
\uuuu str\uuuu
,还有一种普通的方法,
insert
。通过执行以下操作,首先探索
插入

d1 = Container()
d1.insert(4)
print(d1)
d1.insert(2)
print(d1)
d1.insert(4)
print(d1)
您将获得以下输出:

4:1

2:1
4:1

2:1
4:2
有3组响应,每个
print()
调用一个响应。你能看到发生了什么吗?插入
4
时,会看到
4:1
。如果再次插入
4
,则会看到
4:2
。换句话说,您看到的字符串表示形式是value
count

这是因为
Container
具有成员
vals
,这是一个字典。字典中的每个项都是value
count,就像字符串表示法一样

您的第一个任务是编写一个子类
Bag
,它完成
Container
所做的一切,但也有方法
remove
count

方法
count
只为
容器中的一个值生成与
\uuuu str\uuu
为所有值生成的结果相同的答案。从字典中选择相应的值并返回出现的次数。请注意,可以询问不存在的值的计数:在这种情况下返回
0

class Bag(Container):
    def count(self, e):
        return self.vals.get(e,0)
    def remove(self, e):
        if e not in self.vals:
            return
        self.vals[e] -= 1
        if self.vals[e] < 1:
            del(self.vals[e])
检查此选项是否有效:

d1 = Bag()
d1.insert(4)
d1.insert(4)
print(d1.count(4))
该位的另一半用于写入
删除
,这与
插入
相反。
insert
做什么?如果插入的值已在
VAL
中,则会增加计数,否则会将计数设置为1。因此
remove
需要减少计数,如果计数为零,则从字典中删除该项。请注意,可以尝试删除不存在的值:在这种情况下忽略它

class Bag(Container):
    def count(self, e):
        return self.vals.get(e,0)
    def remove(self, e):
        if e not in self.vals:
            return
        self.vals[e] -= 1
        if self.vals[e] < 1:
            del(self.vals[e])
添加这段代码时要小心。缩进需要与计数对齐


你问题的第三部分实际上是对第一部分的重复。
remove
方法相同。
is_in
方法与
count
方法相同,只是它返回的是
True
False
而不是数字。

纠正缩进。这听起来更像是教授或助教的问题-你基本上只是在这里转储整个问题集。如果你能把它缩小到某个特定的范围,并至少尝试解决这个问题,那么它可能就在这个主题上。否则,这太宽泛了。你第三部分答对了吗?我的问题很长,因为我不理解问题本身。在python 3中,你可以使用
\uuu class\uuu()
而不是
self.\uu class\uu()
你能给出is\u In方法的确切代码吗?甚至不准备尝试,嗯<代码>在self.vals中返回e
谢谢你,博格规则非常详细,答案类型也很有用。还有一个地方第三部分失败了,也许是我的错?d1.插入(4)d1.插入(4)d1.插入(4)d1.删除(4)打印(d1.在(4)中插入)
    def __add__(self, other):
        result = self.__class__()
        result.vals.update(self.vals)
        for value,count in other.vals.items():
            for _ in range(count):
                result.insert(value)
        return result