如何构建字典的字典?

How do I construct a dictionary of dictionary of dictionaries?

我在研究一些基本的基因编码技术时遇到了一个问题:

我的字典应该是这样的:

codon_dict= {
    "A": {
        "A": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "T": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "G": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "C": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        }
    },
    "T": {
        "A": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "T": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "G": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "C": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        }
    },
    "G": {
        "A": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "T": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "G": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "C": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        }
    },
    "C": {
        "A": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "T": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "G": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        },
        "C": {
            "A": 0.0,
            "T": 0.0,
            "G": 0.0,
            "C": 0.0
        }
    }
}

有什么方法可以从列表中构造它,而不是硬编码吗?或者迭代一个 for 循环并构造相同的?

即 code_dict["A"]["A"]["A"] 应该 return 我 0.0

尝试 itertools.product:

from itertools import product

s = "ATGC"

out = {}
for x, y, z in product(s, repeat=3):
    out.setdefault(x, {}).setdefault(y, {})[z] = 0.0

print(out)

打印:

{
    "A": {
        "A": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "T": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "G": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "C": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
    },
    "T": {
        "A": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "T": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "G": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "C": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
    },
    "G": {
        "A": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "T": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "G": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "C": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
    },
    "C": {
        "A": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "T": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "G": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
        "C": {"A": 0.0, "T": 0.0, "G": 0.0, "C": 0.0},
    },
}