删除不等于n个部分的块

2024-06-17 08:07:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把这个序列分成一个n=3的列表

codons('agucaccgucautc')
# result = ['agu','cac','cgu','cau']
# 'tc' is supposed to be ignored as it doesn't equal to n=3

我尝试了以下解决方案

def codons(RNA): 
    """This functions returns a list of codons present in an RNA sequence"""

    # store the length of string
    length = len(RNA)

    #divide the string in n equal parts
    n = 3
    temp = 0
    chars = int(len(RNA)/3)

    #stores the array of string
    change = []

    #check whether a string can be divided into n equal parts
    for i in range(0, length, chars):
        part = [RNA[i:i+3] for i in range(0, length, n)];
        change.append(part);
        return part

        if (length % n != 0):
            continue

但是当我再次尝试运行前面的代码时,它仍然返回'tc'

codons('agucaccgucautc')
# result = ['agu', 'cac', 'cgu', 'cau', 'tc']

有人能帮我做些什么来忽略任何不等于n=3或最后一部分“tc”的字符吗


Tags: oftheinstringequalresultlengthrna