如何加速Python中的字典构建

时间:2016-02-09 17:29:51

标签: python python-3.x dictionary

我查看了链接,但似乎没有任何内容适用。我正在做我认为将在其他地方使用的三个字典的简单构建。它们并不是那么大,但这个功能需要将近4分钟才能完成。我可能会错过一些东西,因为我希望它能跑得更快。这是Python 3.4

class VivifiedDictionary(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

def dict_build(exclude_chrY):
    coordinate_intersection_dict = VivifiedDictionary()
    aberration_list_dict = VivifiedDictionary()
    gene_list_dict = VivifiedDictionary()

    if eval(exclude_chrY):
        chr_y = ""
    else:
        chr_y = "chrY"
    abr_type_list = ["del", "ins"]
    mouse_list = ["chr1", "chr2", "chr3", "chr4", "chr5", "chr6", "chr7", "chr8", "chr9", "chr10", "chr11", "chr12", "chr13", "chr14", "chr15", "chr16", "chr17", "chr18", "chr19", "chrX", chr_y]

    for chrom in mouse_list:
        for aberration in abr_type_list:
            coordinate_intersection_dict[chrom][aberration] = []
            aberration_list_dict[chrom][aberration] = []
            gene_list_dict[chrom][aberration] = []

1 个答案:

答案 0 :(得分:0)

请检查这个Python Profilers我认为这可能有助于找到更大脚本中的瓶颈。