【Python】列表循环修改索引

Python列表修改索引

任务:

  1. 随机数生成
  2. 文件读写
  3. 数据分析, 连续N个数里 1 出现的起止位置
  4. 修改列表的循环索引
  5. 可用于异常数据分析
"""
任务:
1. 随机数生成
2. 文件读写
3. 数据分析, 连续N个数里 1 出现的起止位置 
4. 修改列表的循环索引
5. 可用于异常数据分析

注: 该程序主要用于学习使用, 不推荐用于生产环境
"""

import numpy as np 


def generate_rand_save(low:int=0, high:int=5, num:int=1000):
    """
    生成随机整数, 最小为low, 最大为 high-1, 个数为 num
    生成随机数字并保存下来,方便后续调试使用
    """
    rand_list = np.random.randint(low, high, num)
    with open('rand_save.txt', 'w') as f:
        f.write('\n'.join(map(str, rand_list)))
    print(rand_list)
    return rand_list

def read_rand(filename: str="rand_save.txt"):
    """
    read_rand 
        从文件中读取数据
    Args:
        filename (str, optional): _description_. Defaults to "rand_save.txt".

    Returns:
        _type_: _description_
    """
    rand_list = []
    f = open(filename, "r",encoding='utf-8')
    line = f.readline()
    while line:
        rand_data = int(line)
        rand_list.append(rand_data)
        line = f.readline()
    f.close()
    print("**"*50)
    print(rand_list)
    return rand_list


def find_continue_with_gap(low:int=0, high:int=5, num:int=1000, gap_num:int=5, tag_num: int=4):
    """
    test 连续 gap_num 个数里 tag_num 出现的起止位置, tag_num 之间的间隔小于 gap_num 算连续, 大于 gap_num 结束
    Args:
        gap_num (int, optional): 连续的间隔. Defaults to 5.
        tag_num (int, optional): 标记的数字. Defaults to 1.
    """
    ## 生成1000个随机数(0~5)
    rand_list = generate_rand_save(low, high, num)
    
    # 通过文件读取随机数
    # rand_list = read_rand()
    
    # 片段列表
    patch_list = []
    
    starti, startj = 0, 0
    num = len(rand_list)
    one_num = 0   # 连续 1 的个数
    other_num = 0  # 其他数字连续个数
    end = 0
    while starti < num:
        info_ = {
    
    }
        
        # 如果遇到1就从该处往后找 gap_num 个数
        if rand_list[starti] == tag_num:
            one_num += 1
            other_num = 0
            info_["start"] = starti
            info_["end"] = starti
            startj = starti            
            while startj < num:
                startj += 1
                if startj >= num:
                    end = 1
                    break
                # 如果出现了 1, 增加计数, 并且其他数字连续性置零
                if rand_list[startj] == tag_num:
                    one_num += 1
                    other_num = 0
                    info_["end"] = startj
                else:
                    other_num += 1
                    # 如果其他数字连续超过 gap_num, 并且 1 的个数超过 1, 则插入到列表
                    # 重新调整开始位置
                    if other_num >= gap_num:
                        if one_num > 1:
                            patch_list.append(info_)
                        one_num = 0
                        starti = startj + 1
                        break
            
            
        else:
            starti += 1

        if starti >= num or end == 1:
            if one_num > 1:
                patch_list.append(info_)
            break
    
    print("find *{}* continue with *{}* gap in rand_list".format(tag_num, gap_num))
    for ix in patch_list:
        print(ix)

    
if __name__ == '__main__':
    # find_continue_with_gap()
    find_continue_with_gap(low=5, high=20, num=1000, gap_num=50, tag_num=13)

猜你喜欢

转载自blog.csdn.net/zhoujinwang/article/details/129815909