如何在 Python 中对具有不同起点和终点的字符串进行切片

How to slice a string with different starting and end points in Python

我有一个像 rna = "UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGCUAA" 这样的字符串,我想遍历该字符串并捕获以 'AUG''UAA''UAG' 或开头的不同字符串'UGA'.

这是我到目前为止编写的代码:

rna = "UACGAUGUUUCGGGAAUGCCUAAAUGUUCCGGCUGCUAA"      # start --> AUG; STOP --> UAA, UAG, UGA
hello = " "
n = 3
list = []
for i in range(0, len(rna), n):
    list.append(rna[i:i+n])

for i in list:
    if i == "AUG":
        hello += i
        i = i + 1
        if i != "UAA" or "UAG" or "UGA":
            hello += i

这给我一个类型错误:-

这行代码的问题:

if i != "UAA" or "UAG" or "UGA":
            hello += i

您应该单独检查对象 i 的每个值:

if i!= "UAA" or i!= "UAG" or i!= "UGA":
            hello += i

或者您可以简单地检查条件:

 if i not in ["UAA", "UAG", "UGA"]:
           hello += i

此外,您不能在这行代码中将字符串值与 int 值连接起来:

i = i + 1

您应该将 1 转换为字符串数据类型。