生成嵌套列表的树库

2024-09-30 16:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中创建包含嵌套列表的树时,我遇到了一个问题: 最近,我写了一个函数,它接受一个数字,并通过将其除以10来生成数字补丁:

data = [[0, 250_000_000]]
cycle = 10
Finale_variants_in_file = 200
seprator = '_'


def divider(start, stop, patch, cycle):
    Sliced_list = []
    i = 1
    for t in range(cycle):
        if i == 1:
            left = start
            right = start + patch
            Sliced_list.append([left, right])
        elif i == cycle:
            left = start + (((i - 1) * patch) + 1)
            right = stop
            Sliced_list.append([left, right])
        else:
            left = start + (((i - 1) * patch) + 1)
            right = start + (i * patch)
            Sliced_list.append([left, right])
        i += 1
    return Sliced_list


def chro_pack_main(data, cycle, Finale_variants_in_file):
    for contig in data:
        start, stop = contig
        interval = (stop - start)
        patch = interval // cycle
        if patch >= Finale_variants_in_file:
            new_contig = divider(start, stop, patch, cycle)
            print([contig, new_contig])
            chro_pack_main(new_contig, cycle, Finale_variants_in_file)


chro_pack_main(data, cycle, Finale_variants_in_file)

例如[0,250_000_000]的结果如下:

 [[0, 25000000], [25000001, 50000000], [50000001, 75000000], [75000001, 100000000], [100000001, 125000000], [125000001, 150000000], [150000001, 175000000], [175000001, 200000000], [200000001, 225000000], [225000001, 250000000]]

然后函数递归地运行自己,直到达到一个特定的间隔。并将结果保存为一个文件,其中每一行都是嵌套列表,列表中的索引0是父级,索引1是子级:例如:

[[0, 250000000], [[0, 25000000], [25000001, 50000000], [50000001, 75000000], [75000001, 100000000], [100000001, 125000000], [125000001, 150000000], [150000001, 175000000], [175000001, 200000000], [200000001, 225000000], [225000001, 250000000]]]

[[0, 25000000], [[0, 2500000], [2500001, 5000000], [5000001, 7500000], [7500001, 10000000], [10000001, 12500000], [12500001, 15000000], [15000001, 17500000], [17500001, 20000000], [20000001, 22500000], [22500001, 25000000]]]

[[0, 2500000], [[0, 250000], [250001, 500000], [500001, 750000], [750001, 1000000], [1000001, 1250000], [1250001, 1500000], [1500001, 1750000], [1750001, 2000000], [2000001, 2250000], [2250001, 2500000]]]

[[0, 250000], [[0, 25000], [25001, 50000], [50001, 75000], [75001, 100000], [100001, 125000], [125001, 150000], [150001, 175000], [175001, 200000], [200001, 225000], [225001, 250000]]]

[[0, 25000], [[0, 2500], [2501, 5000], [5001, 7500], [7501, 10000], [10001, 12500], [12501, 15000], [15001, 17500], [17501, 20000], [20001, 22500], [22501, 25000]]]

[[0, 2500], [[0, 250], [251, 500], [501, 750], [751, 1000], [1001, 1250], [1251, 1500], [1501, 1750], [1751, 2000], [2001, 2250], [2251, 2500]]]

[[2501, 5000], [[2501, 2750], [2751, 2999], [3000, 3248], [3249, 3497], [3498, 3746], [3747, 3995], [3996, 4244], [4245, 4493], [4494, 4742], [4743, 5000]]]

[[5001, 7500], [[5001, 5250], [5251, 5499], [5500, 5748], [5749, 5997], [5998, 6246], [6247, 6495], [6496, 6744], [6745, 6993], [6994, 7242], [7243, 7500]]]

[[7501, 10000], [[7501, 7750], [7751, 7999], [8000, 8248], [8249, 8497], [8498, 8746], [8747, 8995], [8996, 9244], [9245, 9493], [9494, 9742], [9743, 10000]]]

事实上,我想把这些信息打包成一个列表树。每个家长有10个孩子。例如,最终列表如下所示:

[parent[children[children[],children[],children[],children[],children[],children[],children[],children[],children[],children[]], children[], children[], children[],children[],children[],children[],children[],children[],children[]]

这有点让人困惑,但我希望我能理解这一点。 我已经尝试过treelib,但它无法生成嵌套列表,如果您将列表作为值插入,它将崩溃

提前谢谢


Tags: inright列表leftstartlistfilepatch