• Python---面向对象编程---自定义列表和集合操作类


    一、定义一个列表的操作类Listinfo

    包括的方法

    1、列表元素添加:add_key()  添加的必须是数字或者是字符串

    2、列表元素取值:get_key()

    3、列表合并:update_list(list)

    4、删除并且返回最后一个元素:del_key()

    --------------------------------------------------------------

    class ListInfo(object):
       
        def __init__(self, list_val):
            self.list = list_val
           
        def add_key(self, key_name):
            self.list.append(key_name)
            print(self.list)
            return "OK"
       
        def get_key(self, index):
            #判断传入的索引是否超出了列表
            if index >= 0 and index < len(self.list):
                return self.list[index]
            return "你输入的太多了,想太多了"
       
        def update_list(self, new_list):
            self.list.extend(new_list)
            return self.list
       
        def del_key(self):
            #首先要判断我们的列表里面还有元素
            if len(self.list) >= 0:
                return self.list.pop(-1)
            else:
                return "列表是空的"
       
    list_info = ListInfo([1,2,3,4,5])
    print(list_info.add_key(6))
    print(list_info.update_list([8,9,10]))
    -------------------------------------------------------------------
    定义一个集合的操作类
    包括的方法
      1、集合元素添加:add_setinfo()
      2、集合的交集:get_intersection()
      3、集合的并集:get_union()
      4、集合的差集:del_difference()
    -------------------------------------------------------
    class SecInfo(object):
        def __init__(self, my_set):
            self.sett = my_set
           
        def add_setinfo(self, keyname):
            self.sett.add(keyname)
            return self.sett
       
        def get_intersection(self, unioninfo):
            if isinstance(unioninfo, set):
                return self.sett & unioninfo
            else:
                return "你传入的不是set"
        def get_union(self, unioninfo):
            return self.sett |  unioninfo
       
        def del_difference(self, unioninfo):
            return self.sett - unioninfo
    A = set([1,2,3,4,5])
    B = set([5,6,3])
    #my_set = SecInfo((1,2,3,4,5))
    #print(my_set.add_setinfo(6))
    my_set = SecInfo(A)
    print(my_set.get_intersection(B))
    print(my_set.del_difference(B))
    ---------------------------------------------
     
     
  • 相关阅读:
    Hive-03 常用函数
    linux定时运行命令脚本crontab
    Flink| time| watermark| Windows窗口
    多个线程运行MR程序时hadoop出现的问题
    maxwell实时同步mysql中binlog
    Hive-04 参数调优
    gopm的使用和更新go语言
    flutter的成功
    数据库系统概论--数据模型
    mysql设置编码格式--支持中文
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/10980491.html
Copyright © 2020-2023  润新知