我的代码运行得不错,但我不明白为什么它只使用第一项表单searchList进行搜索。这是我的代码:
def analyzeSequence(dnastring,searchList):
empty = {}
for item in searchList:
if dnastring.count(item) > 1:
position = dnastring.index(item)
times = dnastring.count(item)
new = position, times
empty[item] = new
return empty
seq = "ATGCGATGCTCATCTGCATGCTGA"
sList = ["CAT","GC"]
print(analyzeSequence(seq,sList))它打印:
{'CAT': (10, 2)}但我想要打印出来:
{'CAT': (10, 2), 'GC': (2, 4)}发布于 2019-11-11 00:19:25
您不能在第一次进入if时返回,只能在结束时返回
def analyzeSequence(dnastring, searchList):
values = {}
for item in searchList:
if dnastring.count(item) > 1:
values[item] = dnastring.index(item), dnastring.count(item)
return values如果你感兴趣,这里是字典理解的方法
def analyzeSequence(dna, searchList):
return {item:(dna.index(item), dna.count(item)) for item in searchList if dna.count(item)>1}https://stackoverflow.com/questions/58790543
复制相似问题