1

I've created a list and a dictionary for calculating amino acid frequencies.

I want to basically replace the values in the dictionary with values in the list.

So for example,

what I have now is.

L1 = [0.0429, 0.0071, 0.05, 0.0929, 0.0429, 0.0143, 0.0071, 0.0429, 0.0929, 0.1143, 0.0643, 0.0643, 0.05, 0.0286, 0.0286, 0.0643, 0.0857, 0.0714, 0.0143, 0.0214]

and

D1 = OrderedDict([('A', 6), ('C', 1), ('D', 7), ('E', 13), ('F', 6), ('G', 2), ('H', 1), ('I', 6), ('K', 13), ('L', 16), ('M', 9), ('N', 9), ('P', 7), ('Q', 4), ('R', 4), ('S', 9), ('T', 12), ('V', 10), ('W', 2), ('Y', 3)])

I want to replace every value in D1 to values in L1. So for example I want ([('A', 0.0429),('C',0.0071).... and so on.

This is the code that creates the list (L1) and the dictionary(D1).

for seq_record in SeqIO.parse(f1,'fasta'):

        sorted_dict = (collections.OrderedDict(sorted(Counter(seq_record.seq).items())))
        total = float(sum(Counter(seq_record.seq).values()))
        print sorted_dict
        aa_frequency =(round(value/total,4) for key, value in sorted_dict.items())
        aa_frequency_value = []
        for i in aa_frequency:
            aa_frequency_value.append(i) 

1 Answer 1

3

Pretty straightforward since you already have the values and keys in order.

for val, key in zip(L1, D1):
    D1[key] = val

Seems like you should probably just do this in-place, since you're not adding or deleting elements

total = float(um(Counter(seq_record.seq).values()))
for key,val in sorted_dict.items():
    newval = round(val/total, 4)
    sorted_dict[key] = newval
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this is exactly what I wanted,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.