一、定义一个列表的操作类:Listinfo
包括的方法:
1 列表元素添加: add_key(keyname) [keyname:字符串或者整数类型]
2 列表元素取值:get_key(num) [num:整数类型]
3 列表合并:update_list(list) [list:列表类型]
4 删除并且返回最后一个元素:del_key()
list_info = Listinfo([44,222,111,333,454,'sss','333'])
1 class Listinfo(object): 2 def __init__(self,List): 3 self.List=List 4 5 def add_key(self,keyname): 6 if not isinstance(keyname,str) and not isinstance(keyname,int): 7 return 'keyname must be a string or a list!' 8 self.List.append(keyname) 9 return self.List 10 11 def get_key(self,num): 12 if not isinstance(num,int): 13 return 'error' 14 return self.List[num] 15 16 def update_list(self,list_new): 17 if not isinstance(list_new,list): 18 return 'error' 19 self.List.extend(list_new) 20 return self.List 21 22 def del_key(self): 23 return self.List.pop() 24 25 26 List=[44,222,111,333,454,'sss','333'] 27 list_2=Listinfo(List) 28 29 print list_2.add_key(5) 30 print list_2.get_key(1) 31 print list_2.update_list([3,5]) 32 print list_2.del_key()
二、定义一个集合的操作类:Setinfo
包括的方法:
1 集合元素添加: add_setinfo(keyname) [keyname:字符串或者整数类型]
2 集合的交集:get_intersection(unioninfo) [unioninfo :集合类型]
3 集合的并集: get_union(unioninfo)[unioninfo :集合类型]
4 集合的差集:del_difference(unioninfo) [unioninfo :集合类型]
set_info = Setinfo(你要操作的集合)
1 #!/use/bin/python 2 #-*- coding:UTF-8 -*- 3 4 class Setinfo(object): 5 def __init__(self,set_info): 6 self.set_info=set_info 7 8 def add_setinfo(self,keyname): 9 if not isinstance(keyname,str) and not isinstance(keyname,int): 10 return 'keyname must be a string or a int!' 11 self.set_info.add(keyname) 12 return set_info 13 14 def get_intersection(self,unioninfo): 15 if not isinstance(unioninfo,set): 16 return 'error' 17 return self.set_info&unioninfo 18 19 def get_union(self,unioninfo): 20 if not isinstance(unioninfo,set): 21 return 'error' 22 return set_info|unioninfo 23 24 def del_difference(self,unioninfo): 25 return self.set_info-unioninfo 26 27 28 29 30 if __name__=='__main__': 31 set_info=set(['WR','FE',4,6]) 32 33 set_2=Setinfo(set_info) 34 print set_2.add_setinfo(3) 35 36 set_3=set([5,6]) 37 print set_2.get_intersection(set_3) 38 39 set_4=set([5,6]) 40 print set_2.get_union(set_4) 41 42 set_5=set([5,6]) 43 print set_2.del_difference(set_5)