Python 使用list实现简单的set
来源:互联网 发布:实验数据记录怎么写 编辑:程序博客网 时间:2024/04/30 22:19
# Implementation of iterclass _SetIterator: def __init__(self, theList): self._setItems = theList self._curItem = 0 def __iter__(self): return self def next(self): if self._curItem < len(self._setItems): item = self._setItems[self._curItem] self._curItem += 1 return item else: raise StopIteration # Implementation of the Set ADT container using a Python listclass mySet: # Creates an empty set instance def __init__(self): self._theElements = list() # Returns the number of items in the set def __len__(self): return len(self._theElements) # Determines if an element is in the set def __contains__(self, element): return element in self._theElements # Adds a new unique element to the set def add(self, element): if element not in self: self._theElements.append(element) # Removes an element from the set def remove(self, element): assert element in self, "The element must be in the set." self._theElements.remove(element) # Determines if two sets are equal def __eq__(self, setB): if len(self) != len(setB): return False else: return self.isSubsetOf(setB) # Determines if this set is a subset of setB def isSubsetOf(self, setB): for element in self: if element not in setB: return False return True # Creates a new set from the union of this set and setB def union(self, setB): newSet = mySet() newSet._theElements.extend(self._theElements) for element in setB: if element not in self: newSet._theElements.append(element) return newSet # Creates a new set from the intersection def interset(self, setB): newSet = mySet() for element in setB: if element in self: newSet._theElements.append(element) return newSet def difference(self, setB): newSet = mySet() for element in self: if element not in setB: newSet._theElements.append(element) return newSet # Returns an iterator for traversing the list of items def __iter__(self): return _SetIterator(self._theElements)
def test_linearset(): # import mySet from linearset import mySet # init a set named smith smith = mySet() smith.add('CSCI-112') smith.add('MATH-121') smith.add('HIST-340') smith.add('ECON-101') # init a set named roberts roberts = mySet() roberts.add('POL-101') roberts.add('ANTH-230') roberts.add('CSCI-112') roberts.add('ECON-101') for a in smith: print a if __name__ == "__main__": test_linearset()
0 0
- Python 使用list实现简单的set
- Python 使用list实现简单的集合set
- Python 使用有序list构建简单的set
- Python 使用list实现简单的堆栈
- Python 使用list实现简单的map
- STL------list、set、map的简单使用
- python的dict,set,list,tuple简单分析
- python的dict,set,list,tuple简单分析 老搞混
- list,set的使用
- Java中,简单的List转Set实现
- Python中list、tupe、dic、set的使用小记
- Python List使用的简单心得
- 蓝桥杯.取字符(去掉重复的使用set list实现)
- Python set()的使用
- List 、Set 和Map的简单讲解
- Python - list, tuple, dict, set的区别
- Python的list、tuple、dict、set
- python-str,list,set间的转换
- Android Scroller简单用法
- sping 定时器 掉存过过程
- 如何从网页获取验证码并显示在窗口控件上呢?
- Qwt例子-CpuPlot注释版
- TextView 所有属性介绍
- Python 使用list实现简单的set
- Java基础----多态
- C# 处理事务
- 淘宝海量数据产品技术架构
- android 文件读取(assets)
- 1412202049-hpu-1004:C语言考试练习题_保留字母
- CentOS 6.6/Fedora 21 源码编译安装 mysql 5.6.22
- monkey测试一些常用的命令
- Qwt直方图例子注释