简体   繁体   中英

Elements from two dictionaries into list

I am reading a file and tallying the number of amino acids in an entire fasta file. Thus far, all is working well. I then need to calculate percentages, and output the highest five, with amino acid abbreviation, count tally and percentage. I have all the elements but am having trouble after that. I possess two dictionaries, same keys in both with different values (tally and percentage). I was trying to combine the two dictionaries into a single list but am struggling.

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())

counts dictionary

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

centages dictionary

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

This is where I'm stuck - I have the first dictionary elements into the list, but need to add centages.values into the appropriate location. I have been trying this:

    #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

Current output:

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

So, the last element I need to add is not adding. I'm pretty sure it's something simple.

Expected output should be: ['A', 123885, 9.55], [etc]

If you are just trying to get a nested list with sublists each consisting of a key and the corresponding values from each dict, then you could do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM