本文介绍了从另一本词典更改子词典的键的值-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在此代码中,我试图创建一个包含一个字母氨基酸代码作为键的字典.对于每个键,都有一个与第一个键相同的子词典,只是该子词典键的值应该是在字典ref2
中指示的firstkey/subkey
出现次数.
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)
输出:
{'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}}
推荐答案
您可以使用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}
输出:
{'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}}
这篇关于从另一本词典更改子词典的键的值-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!