Reputation: 99
I have seen this question as a duplicate (seems all my questions have been duplicates so far)
My code
#UAA + UAG + UGA = STOP
n = 3
xdict = {
"Phenylalanine": ["UUU", "UUC"],
"Leucine": ["UUA", "CUU", "CUC", "CUA", "CUG", "UUG"],
"Isoleucine": ["AUU", "AUC", "AUA"],
"Methionine": "AUG",
"Valine": ["GUU", "GUC", "GUA", "GUG"],
"Serine": ["UCU", "UCC", "UCA", "UCG"],
"Proline": ["CCU", "CCC", "CCA", "CCG"],
"Threonine": ["ACU", "ACC", "ACA", "ACG"],
"Alanine": ["GCU", "GCC", "GCA", "GCG"],
"Tyrosine": ["UAU", "UAC"],
"Histidine": ["CAU", "CAC"],
"Glutamine": ["CAA", "CAG"],
"Asparagine": ["AAU", "AAC"],
"Lysine": ["AAA", "AAG"],
"Asparatic Acid": ["GAU", "GAC"],
"Glutamic Acid": ["GAA", "GAG"],
"Cysteine": ["UGU", "UGC"],
"Trytophan": "UGG",
"Arginine": ["CGU", "CGC", "CGA", "CGG", "AGG", "AGA"],
"Serine": ["AGU", "AGC"],
"Glycine": ["GGU", "GGC", "GGA", "GGG"]
}
lookup_dict = {k: key for key, values in xdict.items() for k in values}
a = input("Enter your DNA sequence: ")
a = a.upper()
print("Your DNA sequence is", a)
str(a)
RNA = a.replace("C", "G")
RNA = a.replace("A", "U")
RNA = a.replace("T", "A")
print("Your RNA strand is", RNA)
b = len(a)
if b % 3 == 0:
for k in [a[i:i + n] for i in range(0, len(a), n)]:
if k in xdict.values(): #checking from other question
print(lookup_dict[k], end=" ")
elif k not in xdict.values(): #checking from other question
print("I hate u")
elif b % 3 != 0:
print("Try again.")
I have tried the answer from this link and it doesn't work for me. How do I detect if a value is in a dictionary in python?
Upvotes: 0
Views: 105
Reputation: 191728
You're checking if a string is in a list of lists plus a few strings, so it would return false unless you try to search for those lone strings .
You'd either have to lookup the key, then check if your string is in that key's list value, or loop over all values (which you're already doing) instead like so, then check if it's in the list of the dictionary value
if b % 3 == 0:
for _, values in xdict.items():
for k in (a[i:i + n] for i in range(0, len(a), n)):
if k in values:
print(lookup_dict[k], end=" ")
else
print("I hate u")
Upvotes: 2
Reputation: 7214
You can do this with pandas, it's a good approach for searching like this:
import pandas as pd
df = pd.DataFrame(list(xdict.items()))
def lookupKey(df, key):
return df[df[1].apply(lambda x: True if key in x else x) == True][0].reset_index()[0][0]
lookupKey(df, 'CGU')
# 'Arginine'
Upvotes: 0