列表的自定义工具helper

  1 """
  2     针对列表的自定义工具
  3 """
  4 
  5 
  6 class ListHelper:
  7 
  8     @staticmethod
  9     def find_all(target, func_condition):
 10         """
 11             查找列表中满足条件的所有元素
 12         :param target: 列表
 13         :param func_condition: 条件
 14             函数/方法类型
 15             -- 参数:列表中的元素
 16             -- 返回值:是否满足条件bool值
 17         :return:
 18         """
 19         for item in target:
 20             if func_condition(item):
 21                 yield item
 22 
 23     @staticmethod
 24     def first(target, func_condition):
 25         """
 26             查找列表中满足条件的第一个元素
 27         :param target:
 28         :param func_condition:
 29         :return:
 30         """
 31         for item in target:
 32             if func_condition(item):
 33                 return item
 34 
 35     @staticmethod
 36     def select(target, func_condition):
 37         """
 38             筛选列表中指定条件的数据
 39         :param target:
 40         :param func_condition:
 41         :return:
 42         """
 43         for item in target:
 44             yield func_condition(item)
 45 
 46     @staticmethod
 47     def sum(target, func_condition):
 48         """
 49             计算列表中指定条件的所有元素和
 50         :param target:
 51         :param func_condition:
 52         :return:
 53         """
 54         sum_value = 0
 55         for item in target:
 56             # sum_value += xxx(item)
 57             sum_value += func_condition(item)
 58         return sum_value
 59 
 60     @staticmethod
 61     def last(target, func_condition):
 62         """
 63             按条件找最后一个
 64         :param target:
 65         :param func_condition:
 66         :return:
 67         """
 68         for i in range(len(target) - 1, -1, -1):
 69             if func_condition(target[i]):
 70                 return target[i]
 71 
 72     @staticmethod
 73     def get_count(target, func_condition):
 74         """
 75             获取满足一定条件的元素个数
 76         :param target: 要选择的列表
 77         :param func_condition: 条件
 78             传入函数/方法
 79             参数:列表中的元素
 80             返回值:满足条件的元素总数
 81         :return:
 82         """
 83         count_value = 0
 84         for item in target:
 85             if func_condition(item):
 86                 count_value += 1
 87         return count_value
 88 
 89     @staticmethod
 90     def exists(target, func_condition):
 91         """
 92             查找列表中是否包含某个元素
 93         :param target:列表
 94         :param func_condition:
 95         :return:bool类型值
 96         """
 97         for item in target:
 98             if func_condition(item):
 99                 return True
100         return False
101 
102     @staticmethod
103     def delete_all(target, func_condition):
104         """
105             删除满足条件的对象
106         :param target:
107         :param func_condition:
108         :return: 删除的个数
109         """
110         del_count = 0
111         for i in range(len(target) - 1, -1, -1):
112             if func_condition(target[i]):
113                 del target[i]
114                 # target.remove(target[i])
115                 del_count += 1
116         return del_count
117 
118     @staticmethod
119     def get_max(target, func_condition):
120         """
121             按条件找最大值
122         :param target:
123         :param func_condition:
124         :return:
125         """
126         max_value = target[0]
127         for i in range(1, len(target)):
128             if func_condition(target[i]) > func_condition(max_value):
129                 max_value = target[i]
130         return max_value
131 
132     @staticmethod
133     def get_min(target, func_condition):
134         """
135             按条件找最小值
136         :param target:
137         :param func_condition:
138         :return:
139         """
140         min_value = target[0]
141         for i in range(1, len(target)):
142             if func_condition(target[i]) < func_condition(min_value):
143                 min_value = target[i]
144         return min_value
145 
146     @staticmethod
147     def order_by_up(target,func_condition):
148         """
149             对列表进行升序排列
150         :param target:
151         :param func_condition:
152         :return:
153         """
154         for i in range(len(target)-1):
155             for n in range(i+1,len(target)):
156                 if func_condition(target[n])<func_condition(target[i]):
157                     target[n] ,target[i]=target[i],target[n]
158 
159     @staticmethod
160     def order_by_down(target, func_condition):
161         """
162             对列表进行降序排列
163         :param target:
164         :param func_condition:
165         :return:
166         """
167         for i in range(len(target) - 1):
168             for n in range(i + 1, len(target)):
169                 if func_condition(target[n]) > func_condition(target[i]):
170                     target[n], target[i] = target[i], target[n]
171 
172     @staticmethod
173     def sort(target,func_condition):
174         """
175             对列表进行万能排序
176         :param target: 要处理的列表
177         :param func_condition:
178             传入参数两个,分别为两个元素
179         :param reverse:
180         :return: 查看见原列表
181         """
182         for i in range(len(target) - 1):
183             for n in range(i + 1, len(target)):
184                 if func_condition(target[n],target[i]):
185                     target[n], target[i] = target[i], target[n]

 

posted @ 2019-04-29 23:24  kavin_li  阅读(57)  评论(0编辑  收藏  举报