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

你可能感兴趣的:(python,自动化,开发语言)