zoukankan      html  css  js  c++  java
  • python 集合

     面向对象的集合:

    #coding:utf-8
    __author__ = 'similarface'
    class Set:
        '''
        list实现集合,及其集合操作
        '''
        def __init__(self,value=[]):
            self.data=[]
            self.concat(value)
    
        def intersect(self,other):
            '''
            集合交集操作
            :param other:
            :return:交集
            '''
            res=[]
            for x in self.data:
                #x in 会调用__getitem__
                if x in other:
                    res.append(x)
            return Set(res)
    
        def union(self,other):
            '''
            集合并集操作
            :param other:
            :return:并集
            '''
            #复制自身的一个列表
            res=self.data[:]
            for x in other:
                if not x in res:
                    res.append(x)
            return Set(res)
    
        def concat(self,value):
            for x in value:
                if not x in self.data:
                    self.data.append(x)
    
        def __len__(self):
            return len(self.data)
    
        def __getitem__(self, item):
            return self.data[item]
    
        def __and__(self, other):
            return self.intersect(other)
    
        def __or__(self, other):
            return self.union(other)
    
        def __repr__(self):
            return '<Set:'+repr(self.data)+'>'
    
    if __name__=="__main__":
        users1=Set(['jpan','ch','en'])
        users2=Set(['ta','ch','hk'])
        a=users1 & users2
        b=users1 | users2
    
        tf='ch' in users1
        a.data
        b.data
    
    #coding:utf-8
    __author__ = 'similarface'
    from DataStruct.set import Set
    '''
    使用字典将集合性能优化为线性的  该类继承上面的Set类
    '''
    class Set(Set):
        def __init__(self,value=[]):
            self.data={}
            self.concat(value)
    
        def concat(self,value):
            for x in value:
                self.data[x]=None
    
        def intersect(self,other):
            '''
            求交集操作
            :param other:
            :return:交集
            '''
            res={}
            for x in other:
                if x in self.data:
                    res[x]=None
            return Set(res.keys())
    
        def union(self,other):
            '''
            求并集操作
            :param other:
            :return:并集
            '''
            res={}
            for x in other:
                res[x]=None
            for x in self.data.keys():
                res[x]=None
            return Set(res.keys())
    
        def __getitem__(self, item):
            return list(self.data.keys())[item]
    
        def __repr__(self):
            return '<Set:%r>' % list(self.data.keys())
    
    if __name__=="__main__":
        users1=Set(['jpan','ch','en'])
        users2=Set(['ta','ch','hk'])
        a=users1 & users2
        b=users1 | users2
    
        tf='ch' in users1
        a.data
        b.data
  • 相关阅读:
    Centos 7 开放查看端口 防火墙关闭打开
    idea将项目导出为war包
    webstorm 注册服务器
    centos 6.4系统双网卡绑定配置详解
    centos所有版本镜像下载地址
    浅谈Nginx负载均衡与F5的区别
    勒索病毒应急响应计划
    Python网络编程常用代码
    Flask debug 模式 PIN 码生成机制安全性研究笔记
    Pythonic定义
  • 原文地址:https://www.cnblogs.com/similarface/p/5127593.html
Copyright © 2011-2022 走看看