ListHelper

博客主要整理了Python中列表的操作,包括遍历、排序、取最值等。介绍了自定义操作和内置方法,如append、sort等,还提及高阶函数max、min。指出自定义方法在复杂列表操作时更便捷,同时提到数据结构中也有列表操作,如堆排序等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于列表的操作真的太多的,遍历,排序,拿最大最小值,反正一顿操作,一个你想要的列表就可以打印出来了,我整理了一部分。关于列表的一些自定义操作。当然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的使用。
其实还有很多对列表的操作,这里只写了一部分。包括数据结构中有更多对列表的操作,不过数据结构相对来说更抽象一点,不过这个都需要自己慢慢理解,类似于堆排序,希尔排序,插入排序等,他们同样可以实现对一个列表的排序,是思考问题的方式不一样而已。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值