Home > database >  Counting amino acid numbers and the max and min in a string
Counting amino acid numbers and the max and min in a string

Time:02-17

I have a doubt about a string that I have. My code is:

aminoacidos="IEKTENEVLDEKNSKLFSALLTGINRAFPFAQIPASVYEVHMETLFKITHSSNFNTSIQALVLINQVTVKAKLNSDRYYRTLYESLFDPRLVNSSKQGIYLNLLYKSLKQDALNVERVEA"
list_aminoacidos="DERKNHQSTAGVPLFYIMWC"

print("1. Repeated amino acids ", aminoacidos)
counts={i:0 for i in list_aminoacidos}
for amino in aminoacidos:
    if amino in counts:
        counts[amino]  = 1
for k,v in counts.items():
    print(k,v)

print("2. Max and min: ")
print(min(counts))
print(max(counts))

As you can see it counts the number of each amino acid, but I don't know what it happens when I write min and max command because I get A and Y. However, the program should show L and W, C.

Thank you in advance

CodePudding user response:

I believe your min and max is actually based on the keys of the dictionary and not the values of those keys. Hence, A is the minimum because it is the lowest value because A<B..<Y. Furthermore, Y is the maximum because Y>X..>A.

I used this method to get your desired output. Could be further refined I imagine.

print("2. Max and min: ")
min_keys = []
max_keys = []
min_value = min(counts.items(), key=lambda x: x[1])[1]
max_value = max(counts.items(), key=lambda x: x[1])[1]
for k, v in counts.items():
    if v == min_value:
        min_keys.append(k)
    elif v == max_value:
        max_keys.append(k)
print(min_keys)
print(max_keys)

CodePudding user response:

You see this result because counts is a dictionary. When you do not specify anything else, iterations over a dictionary iterate over the keys, not the values.

A useful class from the standard library that may help you is collections.Counter:

from collections import Counter
counts = Counter(aminoacidos)

for item in counts.most_common():
    print(item)

print(counts.most_common()[-1][0])  # Will print the key of the least common item
print(counts.most_common()[0][0])   # Will print the key of the most common item

CodePudding user response:

Your code gives the min and max letter, which would be A and Y. Try:

print("2. Max and min: ")
max_dic={i:x for i, x in counts.items() if x == max(counts.values())}
min_dic={i:x for i, x in counts.items() if x == min(counts.values())}
print(max_dic)
print(min_dic)
  • Related