来自两个字典的元素进入列表

时间:2019-04-16 20:58:04

标签: python list dictionary

我正在读取文件,并计算整个fasta文件中的氨基酸数量。到目前为止,一切都运转良好。 然后,我需要计算百分比,并输出最高的五个,带有氨基酸缩写,计数和百分比。我拥有所有要素,但是之后遇到了麻烦。我有两个字典,相同的键都有不同的值(计数和百分比)。我试图将这两个词典合并为一个列表,但是却很挣扎。

counts = {}

#open file, read line at a time
for line in open('e_coli_k12_dh10b.faa', 'r'):
line = line.rstrip()    
#ignore header line
if line.startswith('>'):continue
for aa in line:
    #if key in dict, add 1
    if aa in counts:
        counts[aa] += 1           
    #else, (if empty) for aa, set to 1
    else:
        counts.update({aa:1})

#get sum of all dictionary values
total = sum(counts[item] for item in counts)

#iterate over values, add to dictionary divided by total * 100
#new dict for percentages
centages = {}

for aa, tally in counts.items():
    #maths for percentages
    percent = (1. * tally / total * 100)  

#within for loop, add aa and percent to centages    
centages.update({aa: percent})


print(counts.keys())
print(counts.values())
print(centages.keys())
print(centages.values())

数字典

['A', 'C', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'M', 'L', 'N', 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X']

[123885, 14983, 74992, 66618, 95475, 50554, 77836, 29255, 57151, 36759, 139002, 50492, 57732, 57595, 74803, 71819, 3, 69645, 20019, 91683, 36836, 1]

百分比字典

['A', 'C', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'M', 'L', 'N', 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X']

[9.550641489186193, 1.1550814177057491, 5.781343234104621, 5.1357681295282385, 7.360435050087193, 3.8973493953611724, 6.000595156413581, 2.2553498548342583, 4.405930594894298, 2.8338542236832165, 10.71605334204996, 3.8925696417805975, 4.450721511512268, 4.44015979795519, 5.766772694963835, 5.5367277806987385, 0.0002312783990600846, 5.369128034179864, 1.5433207569279443, 7.068099153675245, 2.839790369259092, 7.709279968669486e-05]

这是我遇到的问题-我将第一个字典元素添加到列表中,但需要将centages.values添加到适当的位置。我一直在尝试:

    #for loop to set aa to list as keys - [counts.keys, 
counts.values, centages.values]
L = []
for aa, tally in counts.items():
    L.append([aa, tally])

#add centages.values to list L at aa
for i in range(len(counts)):
    for aa, percent in centages.items():
        if(L[i] == centages.keys):
            L[i].append(centages.values)

print(L)    #just aa, tally so far

当前输出:

[['A', 123885], ['C', 14983], ['E', 74992], ['D', 66618], ['G', 95475], ['F', 50554], ['I', 77836], ['H', 29255], ['K', 57151], ['M', 36759], ['L', 139002], ['N', 50492], ['Q', 57732], ['P', 57595], ['S', 74803], ['R', 71819], ['U', 3], ['T', 69645], ['W', 20019], ['V', 91683], ['Y', 36836], ['X', 1]]

因此,我需要添加的最后一个元素是不添加。我很确定这很简单。

预期输出应为: ['A',123885,9.55],[etc]

1 个答案:

答案 0 :(得分:2)

如果您只是想获取一个包含子列表的嵌套列表,每个子列表都包含一个键和每个字典中的对应值,那么您可以执行以下操作:

counts = {'A': 123885, 'C': 14983, 'E': 74992, 'D': 66618, 'G': 95475, 'F': 50554, 'I': 77836, 'H': 29255, 'K': 57151, 'M': 36759, 'L': 139002, 'N': 50492, 'Q': 57732, 'P': 57595, 'S': 74803, 'R': 71819, 'U': 3, 'T': 69645, 'W': 20019, 'V': 91683, 'Y': 36836, 'X': 1}  
centages = {'A': 9.550641489186193, 'C': 1.1550814177057491, 'E': 5.781343234104621, 'D': 5.1357681295282385, 'G': 7.360435050087193, 'F': 3.8973493953611724, 'I': 6.000595156413581, 'H': 2.2553498548342583, 'K': 4.405930594894298, 'M': 2.8338542236832165, 'L': 10.71605334204996, 'N': 3.8925696417805975, 'Q': 4.450721511512268, 'P': 4.44015979795519, 'S': 5.766772694963835, 'R': 5.5367277806987385, 'U': 0.0002312783990600846, 'T': 5.369128034179864, 'W': 1.5433207569279443, 'V': 7.068099153675245, 'Y': 2.839790369259092, 'X': 7.709279968669486e-05}

results = [[key, counts[key], centages[key]] for key in counts]

print(results)
# [['A', 123885, 9.550641489186193], ['C', 14983, 1.1550814177057491], ['E', 74992, 5.781343234104621], ['D', 66618, 5.1357681295282385], ['G', 95475, 7.360435050087193], ['F', 50554, 3.8973493953611724], ['I', 77836, 6.000595156413581], ['H', 29255, 2.2553498548342583], ['K', 57151, 4.405930594894298], ['M', 36759, 2.8338542236832165], ['L', 139002, 10.71605334204996], ['N', 50492, 3.8925696417805975], ['Q', 57732, 4.450721511512268], ['P', 57595, 4.44015979795519], ['S', 74803, 5.766772694963835], ['R', 71819, 5.5367277806987385], ['U', 3, 0.0002312783990600846], ['T', 69645, 5.369128034179864], ['W', 20019, 1.5433207569279443], ['V', 91683, 7.068099153675245], ['Y', 36836, 2.839790369259092], ['X', 1, 7.709279968669486e-05]]