此代碼將充分利用你的函數,它會產生長度10注200個序列我添加了一行進口隨機改變調用隨意random.random。我也將xrange更改爲範圍,因爲我使用Python 3.x測試了代碼。
讓我知道如果你腦海中還有不同的從第一次印刷的呼叫 AACCGTCTCT
從第二打印通話 [ 'CCAGTTCGGA' 輸出
import random
def randABCD(n, probA, probT, probC, probG):
# where probA + probT + probC + probG == 1
# n = number of characters in string
# pX = probability of the character
cA = probA
cT = cA + probT
cC = cT + probC
def choose():
r = random.random()
if r < cA:
return 'A'
elif r < cT:
return 'T'
elif r < cC:
return 'C'
else:
return 'G'
return ''.join([choose() for i in range(n)])
print(randABCD(10, .25, .25, .25, .25))
print([randABCD(10, .25, .25, .25, .25) for i in range(200)])
輸出
「 ACGGGAAAGT','CGTGGTAAGT','AACGATTGAG','GAGGATATGC','AGTGCCTTGT','TGACTTGCAC','GAAGGAGGCA','TCCGGTAGTT','TCTGCCGTCG','TACATAAGTC','GCTGGTTAAC','CACCCAGGCC','CAAGAGCCAA' ,'GCTATTCGAT','GTGCTCATCT', 'AAAGCAATAC','GTATGGAAAC','GTATTGGTAA','TGTAATCTTA','TCGAATACAT','TCCTCAATGG','TGTAACGGCA','TAGTCACTGT','CAAAGCTCAT','GTTGAAAGTC','CTATCATGAG','CAAGCACTAT','CTGGGCTGCC ','CATGTCCAGG','ACGTGTGATC','AATATGCAAC','ACTGATGGAT','TATCGCGCGA','GTAGACCCAA','CAGGATGCAT','TACGGCAGAG','TATTTTATCA','GGTAATCACA','TAAACGTATG','CTTCCACGCG', GGCTCCAAAA,CAAGAATAAC,TCACGGTCTT,AGCGCGTCGA,TCACTATCAT,TCTGATGTCA,AGAAGGTCGT,TTAGCGTCTC,TGAGATGCGA,ATACCCATGC,ACCGCTCGAG,CCATCAGGCC,AACCTTCCCG ','TCACTCGGGT','CGAGACCGGA','GCAAGATGAT','TGCAATGAGG','CCAGATTGGT','GGCGATGACA','TAGTATGGTT','GCAGGTCTCG','GGTTTTAACC','ACAACCAACT','CTGTTCAGTT','TTGGAGAGTA' 'AGTCGATCTG','TAATGGCAGG','CGTCCTTTAA','GCGCAACTTC','CGGTAGAATG','TCTAGCTTGC','GCGAAAGCGC','GACCCCCGGC','GCAATAGTCT','ATTGACTCCT','ACTAACGCTT','TCATAGAAGC','GTAGCTGCGT ','ACAATCTCCT','TCGACTCTCT','GGCAACAGCA','TATTGTAGAC','GAGGTCAACG','ATGCCAGGGA','CTCTCTTTCT','CTGCGTGATA','GCAA GAATAC,AATGCATGAC,AGTTCAGGCA,GAGATTCCCC,CCGCCGACCA,ACGACGTGCA,TGAACGCCAA,GTCGGCTATT,TGCTTATCAA,AGGAGGCACG,TCTACTGCGA,GCCTTGACAT,GCCTCTCCCC, ,ACACCGACTG,ATTTAATCAT,GTGCAACGTC,GTGTGGCTAA,TGGCGATTAA,GTATGTCTCC,ACTTATGGGC,GCTACGTTTT,ATCCTCACGT,GCCGGCTACA,CCCGTGAAGA,CATGACCACT, GAACCTGATG','ACGAGTGTCA','ATGTTGGTTT','CTTGGAATGA','CTTTCCTCAC','GATGCTCTTT','TAATTCTAAT','TCTGGCAAAG','CCAGGCCGCG','TCATCGCACA','CAGCAAGATT','GCTTAGGAGG','ATATTGTGCG' ,'AATATGACGG','TCTAGTCCCT','GTAAACCGGA','TTTAGCGTAC','CGAATAGAAC','TTAGAATCGG','CGCGCGCCTC','AATGTTAAGG','ACTCGACGCA','GGTTGCTTAC','TCTGGTGCTC','TAATTAGGTA' GCCTTAGAAG,GTTTATACGC,AGCGTCCATA,GTATTGTCGA,CACCTCAGAA,CCCACCTCCG,AGACGCTAGA,CAAAGCCAGA,TCAAATTCAT,GGCCATTTGT,CAACATGGTA,CAAGTGTAAG,CGCCGTAACC, ,'GGGCGGTAAT','GTCCAACCAC','CGAAGCGCAG','GGCGTCGGAG','CTTGTCGCGG','GCCCTTCTGC','TGCAGCCAAC','TGATTTGTTC','TGAATTCAGT ','TAGTCCTGCT','GCCCTATGGG','CCAGGCTGTT','TCCTCAAAAC','CTACGGGCAT','GCCAACCGAG','CAATGGAACT','CCTTATCCTC','TAAAAGGCTA','CGCGTGACAC','TGGCGAGCGT','CCCCGAGCAT' 'CAGCATTCAA','TGTACTGTCC','TCCTTGGTTA','TCAAAGATGT','CGCATACTCA','GAATCTATTT','ATCATAAGGT','ACGCTCTCGC','GTCCTCTTAA','ATCCGAACCT','TGGACTTCCG','TCAGATGATA','ACTTCATGCG ','TCCTATACAA','ATGGTCTTTA','CTAATTCGGT','TGCACCACAT','GTGACCGTCT','ACGTCAGTCA','TCTTCCACCT','CCGAATACGC','ACTATGTCGT','TACTATTCCC','GGGGGACGCA','TGCAGGTTCT', 'GGCTCTGGGG','TGGAGCGCTC','CGAGATCTTA','GGCTTGGCAT']
你想做一件事情200次。你有一個能夠完成這件事的功能。你知道用什麼控制流工具來做一定的事情嗎? – user2357112 2015-04-05 02:39:56