#要根据集合有的功能编写一个类
#自定义集合。模拟Python内置集合类型,实现元素添加、删除以及并集、交集、对称差集等基本运算。
1条回答 默认 最新
关注
- ```python
- class Cuset:
- def __init__(self,seq):
- self._cuset = set(seq)
- def add(self,elem):
- self._cuset.add(elem)
- def remove(self,elem):
- self._cuset.remove(elem)
- def __str__(self):
- return "{0._cuset!s}".format(self)
- def __repr__(self):
- return "Cuset({0._cuset!r})".format(self)
- def update(self,*others):#并集
- for new in others:
- self._cuset |= new._cuset
- def intersection_update(self,*others):#合集
- for new in others:
- self._cuset &= new._cuset
- def difference_update(self,*others):#差集
- for new in others:
- self._cuset -= new._cuset
- def symmetric_difference_update(self,other):
- self._cuset ^= other._cuset
- def clear(self):
- pass
- def pop(self):
- pass
- a=Cuset([1,2,3,4,555,6,6,67])
- b=Cuset(["a","b","c"])
- d=Cuset([66,88,99,100,100,])
- a.update(b,d)
- print(a._cuset)
```
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用 1