美文网首页
Python 对数组切片切份数

Python 对数组切片切份数

作者: Hammer_4030 | 来源:发表于2020-12-17 13:21 被阅读0次

    因工作需要用到多台机器同时计算,所以需要把一个数组数据按照机器数量进行切分,分配均匀。就像扑克发牌一样。

    代码实现

    def cut_list_data(data_list: list, cut_num=1) -> list:
        """
        把数组切成指定份数返回数据数组
        :param data_list: 源数组
        :param cut_num: 切份数
        :return:
        """
        # 数据的份数
        data_nums = len(data_list)
        # 数据分片开始
        cut_start = 0
        # 分片的数量
        # 分片大小,为0表示不分片
        cut_size = data_nums // cut_num
        yu = data_nums % cut_num
        results = []
        for i in range(0, cut_num):
            # 先分余
            if yu > 0:
                yu -= 1
                cut_end = cut_start + cut_size + 1
            else:
                cut_end = cut_start + cut_size
            # 是不是最后的一片
            if cut_end > data_nums - cut_size:
                cut_end = data_nums
            results.append(data_list[cut_start:cut_end])
            cut_start = cut_end
        return results
    
    
    def cut_list_num(data_list: list, cut_num=1) -> list:
        """
        把数组切成指定份数返回数据下标数组
        :param data_list: 源数组
        :param cut_num: 切份数
        :return:
        """
        # 数据的份数
        data_nums = len(data_list)
        # 数据分片开始
        cut_start = 0
        # 分片的数量
        # 分片大小,为0表示不分片
        cut_size = data_nums // cut_num
        yu = data_nums % cut_num
        results = []
        for i in range(0, cut_num):
            # 先分余
            if yu > 0:
                yu -= 1
                cut_end = cut_start + cut_size + 1
            else:
                cut_end = cut_start + cut_size
            # 是不是最后的一片
            if cut_end > data_nums - cut_size:
                cut_end = data_nums
            results.append((cut_start, cut_end))
            cut_start = cut_end
        return results
    
    
    if __name__ == '__main__':
        data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
        print("把数组切成指定份数返回数据数组")
        for i in range(1, 10):
            print(f"------------切成{i}份------------")
            for r in cut_list_data(data, i):
                print(r)
    
        print("把数组切成指定份数返回数据下标数组")
        for i in range(1, 10):
            print(f"------------切成{i}份------------")
            for r in cut_list_num(data, i):
                s, e = r
                print(s, e, "---->", data[s:e])
    
    

    运行结果

    
    把数组切成指定份数返回数据数组
    ------------切成1份------------
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    ------------切成2份------------
    [1, 2, 3, 4, 5]
    [6, 7, 8, 9]
    ------------切成3份------------
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    ------------切成4份------------
    [1, 2, 3]
    [4, 5]
    [6, 7]
    [8, 9]
    ------------切成5份------------
    [1, 2]
    [3, 4]
    [5, 6]
    [7, 8]
    [9]
    ------------切成6份------------
    [1, 2]
    [3, 4]
    [5, 6]
    [7]
    [8]
    [9]
    ------------切成7份------------
    [1, 2]
    [3, 4]
    [5]
    [6]
    [7]
    [8]
    [9]
    ------------切成8份------------
    [1, 2]
    [3]
    [4]
    [5]
    [6]
    [7]
    [8]
    [9]
    ------------切成9份------------
    [1]
    [2]
    [3]
    [4]
    [5]
    [6]
    [7]
    [8]
    [9]
    把数组切成指定份数返回数据下标数组
    ------------切成1份------------
    0 9 ----> [1, 2, 3, 4, 5, 6, 7, 8, 9]
    ------------切成2份------------
    0 5 ----> [1, 2, 3, 4, 5]
    5 9 ----> [6, 7, 8, 9]
    ------------切成3份------------
    0 3 ----> [1, 2, 3]
    3 6 ----> [4, 5, 6]
    6 9 ----> [7, 8, 9]
    ------------切成4份------------
    0 3 ----> [1, 2, 3]
    3 5 ----> [4, 5]
    5 7 ----> [6, 7]
    7 9 ----> [8, 9]
    ------------切成5份------------
    0 2 ----> [1, 2]
    2 4 ----> [3, 4]
    4 6 ----> [5, 6]
    6 8 ----> [7, 8]
    8 9 ----> [9]
    ------------切成6份------------
    0 2 ----> [1, 2]
    2 4 ----> [3, 4]
    4 6 ----> [5, 6]
    6 7 ----> [7]
    7 8 ----> [8]
    8 9 ----> [9]
    ------------切成7份------------
    0 2 ----> [1, 2]
    2 4 ----> [3, 4]
    4 5 ----> [5]
    5 6 ----> [6]
    6 7 ----> [7]
    7 8 ----> [8]
    8 9 ----> [9]
    ------------切成8份------------
    0 2 ----> [1, 2]
    2 3 ----> [3]
    3 4 ----> [4]
    4 5 ----> [5]
    5 6 ----> [6]
    6 7 ----> [7]
    7 8 ----> [8]
    8 9 ----> [9]
    ------------切成9份------------
    0 1 ----> [1]
    1 2 ----> [2]
    2 3 ----> [3]
    3 4 ----> [4]
    4 5 ----> [5]
    5 6 ----> [6]
    6 7 ----> [7]
    7 8 ----> [8]
    8 9 ----> [9]
    

    相关文章

      网友评论

          本文标题:Python 对数组切片切份数

          本文链接:https://www.haomeiwen.com/subject/schzgktx.html