gpt4 book ai didi

python - 将两个字典中的元素放入列表中

转载 作者:行者123 更新时间:2023-12-01 01:02:02 24 4
gpt4 key购买 nike

我正在读取一个文件并计算整个 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], [等]

最佳答案

如果您只是想获取一个带有子列表的嵌套列表,每个子列表由一个键和每个字典中的相应值组成,那么您可以执行以下操作:

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]]

关于python - 将两个字典中的元素放入列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55716503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com