关于列表的操作真的太多的,遍历,排序,拿最大最小值,反正一顿操作,一个你想要的列表就可以打印出来了,我整理了一部分。关于列表的一些自定义操作。当然python中还有自己的内置方法,例如append,sort,pop等很多方法,包括高阶函数max min等这里的方法同样适用,只是多一种思想而已,在很多时候我感觉用自定义方法要比使用内置方法简单,同样整理出来,就是一种一劳永逸的办法,当我们对一个复杂列表操作的时候,我们直接导入就可以了。
class ListHelper:
@staticmethod
def find_all(target, func_condition):
"""
查找列表中满足条件的所有元素
:param target: 列表
:param func_condition: 条件
函数/方法类型
-- 参数:列表中的元素
-- 返回值:是否满足条件bool值
:return:
"""
for item in target:
if func_condition(item):
yield item
@staticmethod
def first(target, func_condition):
"""
查找列表中满足条件的第一个元素
:param target:
:param func_condition:
:return:
"""
for item in target:
if func_condition(item):
return item
@staticmethod
def select(target, func_condition):
"""
筛选列表中指定条件的数据
:param target:
:param func_condition:
:return:
"""
for item in target:
# yield xxx(item)
yield func_condition(item)
@staticmethod
def sum(target, func_condition):
"""
计算列表中指定条件的所有元素和
:param target:
:param func_condition:
:return:
"""
sum_value = 0
for item in target:
# sum_value += xxx(item)
sum_value += func_condition(item)
return sum_value
@staticmethod
def last(target,func_condition):
"""
查找满足条件的最后一个对象
:param target:
:param func_condition:
:return:
"""
for i in range(len(target) - 1,-1,-1):
# if xxx(list01[i]):
if func_condition(target[i]):
return target[i]
@staticmethod
def get_count(target,func_condition):
"""
获取所有满足条件的对象总数
:param target:
:param func_condition:
:return:
"""
count_value = 0
for item in target:
if func_condition(item):
count_value += 1
return count_value
@staticmethod
def exists(target,func_condition):
"""
判断是否包含满足条件的对象
:param target:
:param func_condition:
:return:
"""
for item in target:
if func_condition(item):
return True
return False
@staticmethod
def delete_all(target,func_condition):
"""
删除满足条件的所有对象
:param target:
:param func_condition:
:return:
"""
del_count = 0
for i in range(len(target) - 1,-1,-1):
if func_condition(target[i]):
del target[i]
del_count += 1
return del_count
@staticmethod
def get_max(target,func_condition):
"""
获取指定条件的最大对象(第一个)
:param target:
:param func_condition:
:return:
"""
max_value = target[0]
for i in range(1, len(target)):
# if max_value.hp < target[i].hp:
if func_condition(max_value) < func_condition(target[i]):
max_value = target[i]
return max_value
@staticmethod
def order_by(target,func_condition):
"""
根据指定条件升序排列
:param target:
:param func_condition:
:return:
"""
for r in range(len(target)-1):
for c in range(r+1,len(target)):
# if target[r].hp > target[c].hp:
if func_condition(target[r]) > func_condition(target[c]):
target[r],target[c] = target[c],target[r]
@staticmethod
def get_min(target, func_condition):
"""
获取指定条件的最小对象(第一个)
:param target:
:param func_condition:
:return:
"""
min_value = target[0]
for i in range(1, len(target)):
# if min_value.hp > target[i].hp:
if func_condition(min_value) > func_condition(target[i]):
min_value = target[i]
return min_value
@staticmethod
def order_by_descending(target,func_condition):
"""
根据指定条件降序排列
:param target:
:param func_condition:
:return:
"""
for r in range(len(target)-1):
for c in range(r+1,len(target)):
if func_condition(target[r]) < func_condition(target[c]):
target[r],target[c] = target[c],target[r]
@staticmethod
def sort(target, func_condition):
"""
万能排序
:param target: 需要排序的数据
:param func_condition: 排序的逻辑
func_condition 类型是函数
参数是列表中两个元素
返回值是比较的结果
方法提是比较的条件
:return:
"""
for r in range(len(target) - 1):
for c in range(r + 1, len(target)):
if func_condition(target[r], target[c]):
target[r], target[c] = target[c], target[r]
这里是一部分对列表的操作,func_condition是一个方法,我们把方法作为一个参数传递进来。
这里使用lambda方法对其操作。
result = ListHelper.get_max(list01,lambda e:e.xxx)
print(result)
这里的所有方法都是一个静态方法,同时也需要知道return和yield的使用。
其实还有很多对列表的操作,这里只写了一部分。包括数据结构中有更多对列表的操作,不过数据结构相对来说更抽象一点,不过这个都需要自己慢慢理解,类似于堆排序,希尔排序,插入排序等,他们同样可以实现对一个列表的排序,是思考问题的方式不一样而已。