小编Sha*_*lal的帖子

如何在python中创建多集数据结构

我试图解决一些hackerrank问题,我遇到了一个与multiset相关的问题。我试过这段代码,但我有点困惑我在哪一点上犯了错误?

class Multiset:
    def __init__(self):
        self.items=[]

    def add(self, val):
        # adds one occurrence of val from the multiset, if any
        self.items.append(val)
    

    def remove(self, val):
        # removes one occurrence of val from the multiset, if any
        if len(self.items):
            if val in self.items:
                self.items.remove(val)

    def __contains__(self, val):
        if val in self.items:
            return True
        return False
        
    
    def __len__(self):
        # returns the number of elements in the multiset
        return len(self.items)

if __name__ == '__main__':

Run Code Online (Sandbox Code Playgroud)

set multiset python-3.x

-3
推荐指数
1
解决办法
2万
查看次数

标签 统计

multiset ×1

python-3.x ×1

set ×1