python自动化:列表的处理

用到的第三方库

// 用于判断是否可迭代的库
from collections import Iterable
def getCount(list0, value):
    """
    功能: 统计元素出现的次数。仅支持字符串或数组统计
    :param list0: 可迭代数据
    :param value: 查找的元素
    :return: 统计的次数
    """
    try:
        if (isinstance(list0, str) and isinstance(value, str)) or isinstance(list0, list):
            return list0.count(value)
        return "参数错误"
    except Exception as e:
        return e
def insertValue(list0, index, value):
    """
    功能: 在指定位置添加元素
    :param list0: 数组
    :param index: 位置
    :param value: 值
    :return: 运行结果
    """
    try:
        if isinstance(list0, list):
            list0.insert(index, value)
            return list0
        raise Exception("非法的参数类型")
    except Exception as e:
        return e
def firstInsertValue(list0, value):
    """
    功能: 在数组头部添加元素
    :param list0: 数组
    :param value: 元素
    :return: 运行结果
    """
    return insertValue(list0, 0, value)
def lastInsertValue(list0, value):
    """
    功能:在数组尾部添加元素
    :param list0: 数组
    :param value: 元素
    :return: 运行结果
    """
    try:
        if isinstance(list0, list):
            list0.append(value)
            return list0
        else:
            raise Exception("非法的参数类型")
    except Exception as e:
        return e
def splitArray(list0, start, end):
    """
    功能: 截取数组
    :param list0: 数组
    :param start: 起始位置
    :param end: 结束位置
    :return: 运行结果
    """
    try:
        if isinstance(list0, list) and isinstance(start, int) and isinstance(end, int):
            if start <= -2 and end >= -1:
                return list0[:end]
            elif start >= -1 and end <= -2:
                return list0[start:]
            elif start >= -1 and end >= -1:
                return list0[start:end]
        raise Exception("非法的参数类型")
    except Exception as e:
        return e
def getLength(list0):
    """
    功能: 获取数组长度
    :param list0: 数组
    :return: 长度
    """
    try:
        return len(list0)
    except Exception as e:
        return e
def mergeArray(list0, list1):
    """
    功能: 两个数组合并
    :param list0: 数组1
    :param list1: 数组2
    :return: 合并后的数组
    """
    try:
        if isinstance(list0, list) and isinstance(list1, list):
            return list0 + list1
        raise Exception("非法的参数类型")
    except Exception as e:
        return e
def arrayToStr(list0, sep):
    """
    功能: 将数组按照指定间隔符转为字符串
    :param list0: 数组
    :param sep: 分隔符
    :return: 转化后的结果
    """
    try:
        result = ""
        if isinstance(list0, list):
            for item in list0:
                result += str(item)
                result += sep
            return result[:-2]
        else:
            raise Exception("非法的参数类型")
    except Exception as e:
        return e
def dropArray(list0):
    """
    功能: 数组去重
    :param list0: 数组
    :return: 去重后的数组
    """
    try:
        if isinstance(list0, list):
            return list(set(list0))
        raise Exception("非法的参数类型")
    except Exception as e:
        return e
def judgeInArray(data, value):
    """
    功能: 判断目标是否在字符串/数组中。与字符串中的重复
    :param data: 字符串/数组
    :param value: 目标元素
    :return: bool类型
    """
    try:
        if (isinstance(data, str) and isinstance(value, str)) or isinstance(data, list):
            if value in data:
                return True
            return False
        raise Exception("非法的参数类型")
    except Exception as e:
        return e
def popArray(list0, index=-1):
    """
    删除指定位置的元素,并返回删除的元素。默认删除最后一位
    :param list0: 原数组
    :param index: 要删除的元素的索引
    :return: 删除后的新数组
    """
    try:
        if isinstance(list0, list) and isinstance(index, int):
            if index != -1:
                if len(list0) <= index:
                    raise Exception("非法的参数")
            return list0.pop(index)
        raise Exception("非法的参数")
    except Exception as e:
        return e
def removeData(list0, data):
    """
    删除匹配到的第一个元素
    :param list0: 数组
    :param data: 要删除的元素
    :return: 运行结果
    """
    try:
        if isinstance(list0, list):
            if data in list0:
                list0.remove(data)
                return True
            raise Exception("要删除的对象不在目标数组中")
        raise Exception("非法的参数")
    except Exception as e:
        return e
def sortArray(list0, rules, reverse=False):
    """
    排序。适用于任何可迭代对象
    :param list0: 可迭代对象
    :param rules: 排序依据。如果为对象,可以根据某一对象的属性进行排序
    :param reverse: 排序规则。升序或者降序
    :return: 排序后的数组
    """
    try:
        if isinstance(list0, Iterable):
            return sorted(list0, key=rules, reverse=reverse)
        raise Exception("非法的参数")
    except Exception as e:
        return e
def clearArray(list0):
    """
    清空数组
    :param list0: 数组
    :return: 运行结果
    """
    try:
        if isinstance(list0, list):
            list0.clear()
            return True
        raise Exception("非法的参数")
    except Exception as e:
        return e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿背心的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值