我必须解决一个问题(问题和代码如下),我在执行这个程序时遇到错误。
Traceback (most recent call last):
File "C:/Python33/exercise1.py", line 9, in <module>
for n in range[len(string) - k]:
TypeError: unsupported operand type(s) for -: 'int' and 'list'
-
频繁词问题:在字符串中找到最频繁的 k-mers。
输入:一个字符串 Text 和一个整数 k。[XGMcKJXnoMhmuXcsjswaejVunrrsDhoiWEzXbiKoiYBEpVmhJszFWCFJAHLPzKfKBhWbCABPCTqASGvgquUtnwSeeYkXtLcbzMxvcsUwjmhHfexpEEhjhjzKvYdODZeCgrehxUnYqDwYMBxaFsYAqIFsBSZslMmTNXPovRtRbqFOhMXMUErCnRgjiBIovOWXxlkYInG]
输出:文本中所有最常见的 k-mer。
我设计的代码是:
k=open("dataset_3_6.txt").readlines()
import string
string = str()
for k in string:
k = int(k)
kmer_count = {}
for n in range(len(string) - k):
c = string[n:n+k]
kmer_count[c] = kmer_count[c] + 1 if kmer_count.has_key(c) else 1
max = 0
max_kmer = []
for k,v in kmer_count.iteritems():
if v > max:
max_kmer = [k]
max = v
elif v == max:
max_kmer += [k]
print("max_kmer")