我有一个代码,理论上应该输入一个有错误的DNA,然后删除所有的错误(在我的例子中是N),并计算在那个位置删除了多少个N。你知道吗
我的代码:
class dnaString (str):
def __new__(self,s):
#the inputted DNA sequence is converted as a string in all upper cases
return str.__new__(self,s.upper())
def getN (self):
#returns the count of value of N in the sequence
return self.count("N")
def remove(self):
print(self.replace("N", "{}".format(coolString.getN())))
#asks the user to input a DNA sequence
dna = input("Enter a dna sequence: ")
#takes the inputted DNA sequence, ???
coolString = dnaString(dna)
coolString.remove()
当我输入AaNNNNNNGTC
时,我应该得到AA{6}GTC
作为答案,但当我运行代码时,它会打印出AA666666GTC
,因为我最终用计数替换了每个错误。我怎么才能只输入一次计数呢?你知道吗
从documentation可以预料到:
一种解决方案是使用正则表达式。^{} 可以接受生成替换字符串的可调用字符串:
不是最干净的解决方案,但确实有效
如果要在没有外部库的情况下完成任务,可以使用以下方法:
相关问题 更多 >
编程相关推荐