如何从输入中选择三个连续字母并附加到列表中

时间:2015-10-05 23:28:33

标签: python

askGene=input("Enter Gene:")

ans=askGene.upper()

print(ans)

newStr=''

start=0

end=0

newList=[]

for i in askGene:

    new=ans[start:end]

    newList.append(new)

    start+=3

    end+=3

print(newList)

1 个答案:

答案 0 :(得分:0)

使用列表理解:

askGene = "ABCDEFGHIJKLMNOPQR"
newList = [askGene[i:i+3] for i in range(0, len(askGene), 3)]

这给出了输出:

['ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQR']