Changing subdictionary's key's values from

2019-09-02 15:26发布

In this code I am trying to create a dictionary containing the one letter amino-acid code as keys. For each key, there is a sub-dictionary which is the same as the first one, except that this sub-dictionary key's value should be the number of occurrences of firstkey/subkey which is indicated in the dictionary ref2.

For some reason I don't know yet, my code is giving the wrong values.

AA = ['G', 'A', 'L', 'M', 'F', 'W', 'K', 'Q', 'E', 'S', 'P', 'V', 'I', 'C', 'Y', 'H', 'R', 'N', 'D', 'T']
d = {}
ref2 = {'A/T': 211, 'V/I': 177, 'I/V': 163, 'R/Q': 160, 'T/A': 159, 'P/L': 131, 'R/H': 124, 'A/V': 121, 'V/A': 120, 'Q/R': 115, 'R/C': 105, 'N/S': 100, 'H/R': 98, 'L/P': 97, 'G/S': 93, 'V/M': 91, 'P/S': 88, 'M/V': 88, 'T/I': 87, 'E/K': 85, 'S/N': 82, 'T/M': 79, 'S/G': 79, 'S/P': 79, 'L/V': 76, 'K/R': 76, 'G/R': 74, 'R/G': 73, 'R/W': 73, 'D/N': 71, 'M/T': 68, 'E/D': 66, 'I/T': 65, 'K/E': 63, 'V/L': 61, 'D/E': 58, 'F/L': 58, 'N/D': 54, 'L/F': 53, 'S/T': 52, 'R/K': 51, 'R/S': 51, 'C/R': 50, 'E/G': 49, 'A/S': 46, 'S/L': 45, 'G/D': 45, 'A/P': 43, 'G/A': 43, 'Q/H': 43, 'D/G': 42, 'M/I': 42, 'W/R': 42, 'S/A': 41, 'P/R': 39, 'P/A': 38, 'A/G': 38, 'R/P': 38, 'Q/E': 38, 'E/Q': 37, 'T/S': 36, 'H/Q': 36, 'G/E': 36, 'I/M': 34, 'Y/C': 33, 'K/N': 33, 'S/R': 32, 'L/S': 31, 'H/Y': 31, 'F/S': 30, 'Y/H': 30, 'G/V': 29, 'P/T': 28, 'T/P': 28, 'S/C': 28, 'C/Y': 26, 'N/K': 25, 'K/T': 24, 'R/L': 23, 'M/L': 23, 'T/N': 22, 'S/F': 21, 'C/S': 21, 'Q/K': 21, 'L/M': 20, 'Y/F': 20, 'G/C': 19, 'I/L': 19, 'D/A': 18, 'L/I': 17, 'V/F': 16, 'T/R': 16, 'L/R': 16, 'T/K': 16, 'K/Q': 16, 'E/V': 16, 'D/H': 15, 'Q/P': 15, 'A/D': 14, 'E/A': 14, 'A/E': 13, 'C/G': 13, 'Q/L': 13, 'V/G': 12, 'P/Q': 12, 'D/V': 12, 'S/Y': 12, 'H/N': 12, 'R/T': 12, 'F/I': 12, 'P/H': 11, 'L/H': 11, 'H/P': 11, 'N/T': 11, 'I/N': 11, 'V/D': 10, 'D/Y': 10, 'L/Q': 9, 'C/F': 9, 'N/H': 9, 'Y/D': 8, 'Y/S': 8, 'S/I': 7, 'C/W': 7, 'K/M': 7, 'N/I': 7, 'F/V': 7, 'F/Y': 7, 'I/F': 6, 'H/D': 6, 'N/Y': 6, 'I/S': 5, 'S/W': 5, 'K/I': 5, 'M/K': 5, 'F/C': 5, 'W/G': 5, 'V/E': 4, 'Y/N': 4, 'R/M': 4, 'M/R': 4, 'W/L': 4, 'L/W': 3, 'H/L': 3, 'G/W': 3, 'W/C': 3, 'I/R': 2, 'W/S': 2, 'I/K': 2, 'R/I': 1, 'RE/RK': 1}

for i in AA:
    d.update({i:0})

D = {}
for j in AA:
    D.update({j:d})

for i in D:
    for x in D[i]:
        if (i + '/' + x) in ref2:
            add = i + '/' + x
            D[i][x]= ref2[add]


print (D)

Output:

{'A': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'C': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'E': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'D': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'G': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'F': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'I': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'H': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'K': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'M': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'L': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'N': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'Q': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'P': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'S': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'R': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'T': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'W': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'V': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}, 'Y': {'A': 120, 'C': 33, 'E': 4, 'D': 8, 'G': 12, 'F': 20, 'I': 177, 'H': 30, 'K': 16, 'M': 91, 'L': 61, 'N': 4, 'Q': 160, 'P': 28, 'S': 8, 'R': 42, 'T': 12, 'W': 73, 'V': 76, 'Y': 12}}

2条回答
再贱就再见
2楼-- · 2019-09-02 15:46

You can use itertools.groupby:

import itertools
ref2 = {'A/T': 211, 'V/I': 177, 'I/V': 163, 'R/Q': 160, 'T/A': 159, 'P/L': 131, 'R/H': 124, 'A/V': 121, 'V/A': 120, 'Q/R': 115, 'R/C': 105, 'N/S': 100, 'H/R': 98, 'L/P': 97, 'G/S': 93, 'V/M': 91, 'P/S': 88, 'M/V': 88, 'T/I': 87, 'E/K': 85, 'S/N': 82, 'T/M': 79, 'S/G': 79, 'S/P': 79, 'L/V': 76, 'K/R': 76, 'G/R': 74, 'R/G': 73, 'R/W': 73, 'D/N': 71, 'M/T': 68, 'E/D': 66, 'I/T': 65, 'K/E': 63, 'V/L': 61, 'D/E': 58, 'F/L': 58, 'N/D': 54, 'L/F': 53, 'S/T': 52, 'R/K': 51, 'R/S': 51, 'C/R': 50, 'E/G': 49, 'A/S': 46, 'S/L': 45, 'G/D': 45, 'A/P': 43, 'G/A': 43, 'Q/H': 43, 'D/G': 42, 'M/I': 42, 'W/R': 42, 'S/A': 41, 'P/R': 39, 'P/A': 38, 'A/G': 38, 'R/P': 38, 'Q/E': 38, 'E/Q': 37, 'T/S': 36, 'H/Q': 36, 'G/E': 36, 'I/M': 34, 'Y/C': 33, 'K/N': 33, 'S/R': 32, 'L/S': 31, 'H/Y': 31, 'F/S': 30, 'Y/H': 30, 'G/V': 29, 'P/T': 28, 'T/P': 28, 'S/C': 28, 'C/Y': 26, 'N/K': 25, 'K/T': 24, 'R/L': 23, 'M/L': 23, 'T/N': 22, 'S/F': 21, 'C/S': 21, 'Q/K': 21, 'L/M': 20, 'Y/F': 20, 'G/C': 19, 'I/L': 19, 'D/A': 18, 'L/I': 17, 'V/F': 16, 'T/R': 16, 'L/R': 16, 'T/K': 16, 'K/Q': 16, 'E/V': 16, 'D/H': 15, 'Q/P': 15, 'A/D': 14, 'E/A': 14, 'A/E': 13, 'C/G': 13, 'Q/L': 13, 'V/G': 12, 'P/Q': 12, 'D/V': 12, 'S/Y': 12, 'H/N': 12, 'R/T': 12, 'F/I': 12, 'P/H': 11, 'L/H': 11, 'H/P': 11, 'N/T': 11, 'I/N': 11, 'V/D': 10, 'D/Y': 10, 'L/Q': 9, 'C/F': 9, 'N/H': 9, 'Y/D': 8, 'Y/S': 8, 'S/I': 7, 'C/W': 7, 'K/M': 7, 'N/I': 7, 'F/V': 7, 'F/Y': 7, 'I/F': 6, 'H/D': 6, 'N/Y': 6, 'I/S': 5, 'S/W': 5, 'K/I': 5, 'M/K': 5, 'F/C': 5, 'W/G': 5, 'V/E': 4, 'Y/N': 4, 'R/M': 4, 'M/R': 4, 'W/L': 4, 'L/W': 3, 'H/L': 3, 'G/W': 3, 'W/C': 3, 'I/R': 2, 'W/S': 2, 'I/K': 2, 'R/I': 1, 'RE/RK': 1}
ref3 = {tuple(a.split('/')):b for a, b in ref2.items()}
new_data = [(a, list(b)) for a, b in itertools.groupby(sorted(ref3.items(), key=lambda x:x[0][0]), key=lambda x:x[0][0])]
final_data = {a:{c:d for (_, c), d in b} for a, b in new_data}

Output:

{'Y': {'N': 4, 'C': 33, 'H': 30, 'S': 8, 'D': 8, 'F': 20}, 'A': {'V': 121, 'P': 43, 'S': 46, 'D': 14, 'T': 211, 'E': 13, 'G': 38}, 'S': {'Y': 12, 'A': 41, 'W': 5, 'I': 7, 'F': 21, 'L': 45, 'N': 82, 'C': 28, 'P': 79, 'R': 32, 'G': 79, 'T': 52}, 'V': {'A': 120, 'F': 16, 'E': 4, 'I': 177, 'D': 10, 'L': 61, 'M': 91, 'G': 12}, 'W': {'R': 42, 'C': 3, 'S': 2, 'L': 4, 'G': 5}, 'H': {'Y': 31, 'N': 12, 'Q': 36, 'R': 98, 'P': 11, 'D': 6, 'L': 3}, 'T': {'N': 22, 'I': 87, 'K': 16, 'A': 159, 'R': 16, 'P': 28, 'S': 36, 'M': 79}, 'Q': {'P': 15, 'H': 43, 'R': 115, 'K': 21, 'L': 13, 'E': 38}, 'L': {'I': 17, 'F': 53, 'Q': 9, 'W': 3, 'H': 11, 'R': 16, 'P': 97, 'S': 31, 'V': 76, 'M': 20}, 'M': {'R': 4, 'K': 5, 'I': 42, 'V': 88, 'T': 68, 'L': 23}, 'G': {'A': 43, 'C': 19, 'V': 29, 'W': 3, 'R': 74, 'S': 93, 'D': 45, 'E': 36}, 'N': {'Y': 6, 'K': 25, 'H': 9, 'I': 7, 'S': 100, 'D': 54, 'T': 11}, 'C': {'Y': 26, 'F': 9, 'W': 7, 'R': 50, 'S': 21, 'G': 13}, 'K': {'N': 33, 'Q': 16, 'M': 7, 'R': 76, 'I': 5, 'T': 24, 'E': 63}, 'I': {'N': 11, 'F': 6, 'V': 163, 'R': 2, 'K': 2, 'S': 5, 'T': 65, 'L': 19, 'M': 34}, 'P': {'A': 38, 'Q': 12, 'H': 11, 'R': 39, 'S': 88, 'T': 28, 'L': 131}, 'R': {'W': 73, 'H': 124, 'S': 51, 'L': 23, 'M': 4, 'G': 73, 'C': 105, 'P': 38, 'I': 1, 'K': 51, 'T': 12, 'Q': 160}, 'D': {'Y': 10, 'A': 18, 'V': 12, 'H': 15, 'N': 71, 'E': 58, 'G': 42}, 'F': {'Y': 7, 'C': 5, 'V': 7, 'I': 12, 'S': 30, 'L': 58}, 'RE': {'RK': 1}, 'E': {'A': 14, 'V': 16, 'K': 85, 'Q': 37, 'D': 66, 'G': 49}}
查看更多
够拽才男人
3楼-- · 2019-09-02 15:49

What I think you are looking for is this:

AA = ['G', 'A', 'L', 'M', 'F', 'W', 'K', 'Q', 'E', 'S', 'P', 'V', 'I', 'C', 'Y', 'H', 'R', 'N', 'D', 'T']
ref2 = {'A/T': 211, 'V/I': 177, 'I/V': 163, 'R/Q': 160, 'T/A': 159, 'P/L': 131, 'R/H': 124, 'A/V': 121, 'V/A': 120, 'Q/R': 115, 'R/C': 105, 'N/S': 100, 'H/R': 98, 'L/P': 97, 'G/S': 93, 'V/M': 91, 'P/S': 88, 'M/V': 88, 'T/I': 87, 'E/K': 85, 'S/N': 82, 'T/M': 79, 'S/G': 79, 'S/P': 79, 'L/V': 76, 'K/R': 76, 'G/R': 74, 'R/G': 73, 'R/W': 73, 'D/N': 71, 'M/T': 68, 'E/D': 66, 'I/T': 65, 'K/E': 63, 'V/L': 61, 'D/E': 58, 'F/L': 58, 'N/D': 54, 'L/F': 53, 'S/T': 52, 'R/K': 51, 'R/S': 51, 'C/R': 50, 'E/G': 49, 'A/S': 46, 'S/L': 45, 'G/D': 45, 'A/P': 43, 'G/A': 43, 'Q/H': 43, 'D/G': 42, 'M/I': 42, 'W/R': 42, 'S/A': 41, 'P/R': 39, 'P/A': 38, 'A/G': 38, 'R/P': 38, 'Q/E': 38, 'E/Q': 37, 'T/S': 36, 'H/Q': 36, 'G/E': 36, 'I/M': 34, 'Y/C': 33, 'K/N': 33, 'S/R': 32, 'L/S': 31, 'H/Y': 31, 'F/S': 30, 'Y/H': 30, 'G/V': 29, 'P/T': 28, 'T/P': 28, 'S/C': 28, 'C/Y': 26, 'N/K': 25, 'K/T': 24, 'R/L': 23, 'M/L': 23, 'T/N': 22, 'S/F': 21, 'C/S': 21, 'Q/K': 21, 'L/M': 20, 'Y/F': 20, 'G/C': 19, 'I/L': 19, 'D/A': 18, 'L/I': 17, 'V/F': 16, 'T/R': 16, 'L/R': 16, 'T/K': 16, 'K/Q': 16, 'E/V': 16, 'D/H': 15, 'Q/P': 15, 'A/D': 14, 'E/A': 14, 'A/E': 13, 'C/G': 13, 'Q/L': 13, 'V/G': 12, 'P/Q': 12, 'D/V': 12, 'S/Y': 12, 'H/N': 12, 'R/T': 12, 'F/I': 12, 'P/H': 11, 'L/H': 11, 'H/P': 11, 'N/T': 11, 'I/N': 11, 'V/D': 10, 'D/Y': 10, 'L/Q': 9, 'C/F': 9, 'N/H': 9, 'Y/D': 8, 'Y/S': 8, 'S/I': 7, 'C/W': 7, 'K/M': 7, 'N/I': 7, 'F/V': 7, 'F/Y': 7, 'I/F': 6, 'H/D': 6, 'N/Y': 6, 'I/S': 5, 'S/W': 5, 'K/I': 5, 'M/K': 5, 'F/C': 5, 'W/G': 5, 'V/E': 4, 'Y/N': 4, 'R/M': 4, 'M/R': 4, 'W/L': 4, 'L/W': 3, 'H/L': 3, 'G/W': 3, 'W/C': 3, 'I/R': 2, 'W/S': 2, 'I/K': 2, 'R/I': 1, 'RE/RK': 1}

d = {k:{} for k in AA}
for pairing in ref2:
    d[pairing[0]][pairing[-1]] = ref2[pairing]

Output:

{'G': {'S': 93, 'R': 74, 'D': 45, 'A': 43, 'E': 36, 'V': 29, 'C': 19, 'W': 3}, 'A': {'T': 211, 'V': 121, 'S': 46, 'P': 43, 'G': 38, 'D': 14, 'E': 13}, 'L': {'P': 97, 'V': 76, 'F': 53, 'S': 31, 'M': 20, 'I': 17, 'R': 16, 'H': 11, 'Q': 9, 'W': 3}, 'M': {'V': 88, 'T': 68, 'I': 42, 'L': 23, 'K': 5, 'R': 4}, 'F': {'L': 58, 'S': 30, 'I': 12, 'V': 7, 'Y': 7, 'C': 5}, 'W': {'R': 42, 'G': 5, 'L': 4, 'C': 3, 'S': 2}, 'K': {'R': 76, 'E': 63, 'N': 33, 'T': 24, 'Q': 16, 'M': 7, 'I': 5}, 'Q': {'R': 115, 'H': 43, 'E': 38, 'K': 21, 'P': 15, 'L': 13}, 'E': {'K': 85, 'D': 66, 'G': 49, 'Q': 37, 'V': 16, 'A': 14}, 'S': {'N': 82, 'G': 79, 'P': 79, 'T': 52, 'L': 45, 'A': 41, 'R': 32, 'C': 28, 'F': 21, 'Y': 12, 'I': 7, 'W': 5}, 'P': {'L': 131, 'S': 88, 'R': 39, 'A': 38, 'T': 28, 'Q': 12, 'H': 11}, 'V': {'I': 177, 'A': 120, 'M': 91, 'L': 61, 'F': 16, 'G': 12, 'D': 10, 'E': 4}, 'I': {'V': 163, 'T': 65, 'M': 34, 'L': 19, 'N': 11, 'F': 6, 'S': 5, 'R': 2, 'K': 2}, 'C': {'R': 50, 'Y': 26, 'S': 21, 'G': 13, 'F': 9, 'W': 7}, 'Y': {'C': 33, 'H': 30, 'F': 20, 'D': 8, 'S': 8, 'N': 4}, 'H': {'R': 98, 'Q': 36, 'Y': 31, 'N': 12, 'P': 11, 'D': 6, 'L': 3}, 'R': {'Q': 160, 'H': 124, 'C': 105, 'G': 73, 'W': 73, 'K': 1, 'S': 51, 'P': 38, 'L': 23, 'T': 12, 'M': 4, 'I': 1}, 'N': {'S': 100, 'D': 54, 'K': 25, 'T': 11, 'H': 9, 'I': 7, 'Y': 6}, 'D': {'N': 71, 'E': 58, 'G': 42, 'A': 18, 'H': 15, 'V': 12, 'Y': 10}, 'T': {'A': 159, 'I': 87, 'M': 79, 'S': 36, 'P': 28, 'N': 22, 'R': 16, 'K': 16}}
查看更多
登录 后发表回答