我有这个程序来生成随机的N个序列。
import random
N = 5
def randseq(abc, length):
return "".join([random.choice(abc) for i in range(random.randint(1, length))])
for i in range(N):
print(f'Sequence {i+1}:')
print(randseq("ATCG", 120))我拿到序列了
序列1:
序列2:
序列3:
CACAGCCTTCCTCCAATG。。
序列N:
如何打印最长和最短的N个序列及其长度?
……
发布于 2021-10-09 05:17:59
请检查我的代码。描述就在里面。
import random
def randseq(abc, length):
return "".join([random.choice(abc) for i in range(random.randint(1, length))])
# You should move the input value to the main part of code
# If not, it will treat as global variable
N = 5
# Init the longest seq with shortest one (empty string)
# to make sure that all random seq must longer than this init
longest_seq = ""
# Init the shortest seq with longest one
# (assume that randseq("ATCG", 1000) is long enough)
# to make sure that all random seq must shorter than this init
shortest_seq = randseq("ATCG", 1000)
for i in range(N):
print(f'Sequence {i+1}:')
seq = randseq("ATCG", 120)
# Find the longest one then update it to the longest_seq variable
if len(seq) > len(longest_seq):
longest_seq = seq
# Find the shortest one then update it to the shortest_seq variable
if len(seq) < len(shortest_seq):
shortest_seq = seq
print(seq)
print("")
print('The longest seq is ', longest_seq)
print('The lenght of longest seq is ', len(longest_seq))
print('The shortest is ', shortest_seq)
print('The lenght of shortest seq is ', len(shortest_seq))示例结果(它是随机的,所以当你运行它时,它不会和你一样)
Sequence 1:
CGGTGATCGCGATTACTGCCCGGCCTTGTCCACTCACAGCGATAACAGTGCTTATAGATCTCTCAAGTCTACCGTCTCACCCGTTGATTACCAA
Sequence 2:
AAGGTCAAGATTCGAATTCGTATCGCCGTATGGATAGGCGAAACGAGGGGTGGCTAAGGGGTAGACAGCAGAGCCGCTTTTGTACACCGTAAAACGGACGGTTCAGAACCGGAGGTACG
Sequence 3:
ACGGCCTCATGGATAATGCCCGGGGGAACAGGGAAGGAAAGATTTTGTCAAACTGATTCAGTTAC
Sequence 4:
GATACA
Sequence 5:
ATCGAAAGGAATATCTGTACGGGACGTTTGGTCTCGAGCCTAGCGTAAGCCGCCCGCAATTCGCTCTGATGAGCTACCG
The longest seq is AAGGTCAAGATTCGAATTCGTATCGCCGTATGGATAGGCGAAACGAGGGGTGGCTAAGGGGTAGACAGCAGAGCCGCTTTTGTACACCGTAAAACGGACGGTTCAGAACCGGAGGTACG
The lenght of longest seq is 119
The shortest is GATACA
The lenght of shortest seq is 6预防措施:
在一些(很少)情况下,shortest_seq初始化可能太小(在所有随机序列中最小)。如果发生这种情况,程序将失败。您可以增加randseq输入的长度,以减少遇到此问题的可能性。
例如。
您可以将其更改为:
shortest_seq = randseq("ATCG", 1000)至:
shortest_seq = randseq("ATCG", 10000)https://stackoverflow.com/questions/69503421
复制相似问题