s = "ATATATATAG"
n = 3
def simple(s,n):
dictionary = {}
total = 0
for i in range (len(s)-(n-1)): # (n-1) to get last element
k = i+n
if s[i:k] in dictionary:
dictionary[s[i:k]] += 1
else:
dictionary.update({s[i:k]:1})
total += 1 # doing it here to avoid sum(dictionary.values())
for key, value in dictionary.items():
dictionary[key] = value/total
# As a challenge, edit the line above to lambda function
print(dictionary)
simple(s,n)
# sample output
#{'TAT': 0.375, 'ATA': 0.5, 'TAG': 0.125}
这看起来像作业,但至少是脑筋急转弯的那种。在
提示:}对于此类问题非常方便。在
itertools
、generators
、和{这是一个非常好的算法问题,你自己也可以试试,但这里有一个几乎没有挑战性的解决方案。在
相关问题 更多 >
编程相关推荐