Python Forum

Full Version: Guidance in Creating random output for a variable.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi - This thread is not for problem solving but for giving tools into creating a code for a certain idea (tried to find a solution in certain threads in the forum but to no avail)

so here it is:
I have the current RNA codons, in which the keys stands for the RNA codon and the values stands for the amino acid it translated to. I shall call this dictionary "genetic_code"
genetic_code = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
    "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
    "UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
    "UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
    "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
    "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
    "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
    "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
    "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
    "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
    "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
    "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
    "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
    "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
    "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
    "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G",}
Now, i want to create a new variable (that shall be named "RNA_seq" ).
in this variable, i want to write a code that will pick me 3 random keys from "genetic_code" dictionary, excluding the keys with the value of "STOP". output example:
RNA_seq = ["ACU", "CGU", "CGA"] 
all the possible random keys besides the keys that has "STOP" value

i am sorry if i am not clear enough - still at the start of the road.
many thanks
import random

genetic_code = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
                "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
                "UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
                "UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
                "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
                "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
                "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
                "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
                "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
                "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
                "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
                "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
                "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
                "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
                "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
                "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G", }


def get_3_random_keys_no_stop_values(input_dict):
    while True:
        choices = random.sample(input_dict.keys(), k=3)
        if 'STOP' not in [input_dict[value] for value in choices]:
            return choices
        
print(get_3_random_keys_no_stop_values(genetic_code))
Output:
['GGA', 'ACG', 'GAC']