简体   繁体   中英

sequence of bases with equal probability

I want to create a sequence of the bases (A,T,G,C) which all appear at the same probability. The sequence should have a variable length n. Could you help me?

python has a module named random to choose randomly from a sample.

import random
n = 5 # set n
random_list = random.choices(range(4), k=n)
dic = {0:"A", 1:"C", 2:"G", 3:"T"}
bases_list = [dic[v] for v in random_list]
seq = ""
for item in bases_list:
    seq += item

At first you create a list of n random numbers, all in range [0,3]. Then you use a dictionary to replace numbers with strings representing bases.

Now when we have a list, I just put them all together in one string to create one sequence. That what the last three lines are doing.

As a general note for this site: for next time, give explanation about what you already tried or what difficulties you encountered. The community is glad to help, but not to solve homework for you

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