我的问题可能类似于 this ,而是另一种情况。 在输入中考虑这个列表:
['ACCCACCCGTGG','AATCCC','CCCTGAGG']
另一个输入是n,n是一个数字,是列表中每个元素共有的子串的维数。所以输出必须是带有出现次数的最大出现子字符串,类似于:
{'CCC' : 4}
4 因为在列表的第一个元素中有两次,在其他两个字符串中有一次。CCC 因为是包含 3 个元素的最长子字符串,重复于每个字符串至少 1 次 我是这样开始的:
def get_n_repeats_list(n,seq_list):
max_substring={}
list_seq=list(seq_list)
for i in range(0,len(list_seq)):
if i+1<len(list_seq):
#Idea : to get elements in common,comparing two strings at time
#in_common=set(list_seq[i])-set(list_seq[i+1])
#max_substring...
return max_substring
也许这里是solution
请您参考如下方法:
import operator
LL = ['ACCCACCCGTGG','AATCCC','CCCTGAGG']
def createLenList(n,LL):
stubs = {}
for l in LL:
for i,e in enumerate(l):
stub = l[i:i+n]
if len(stub) == n:
if stub not in stubs: stubs[stub] = 1
else: stubs[stub] += 1
maxKey = max(stubs.iteritems(), key=operator.itemgetter(1))[0]
return [maxKey,stubs[maxKey]]
maxStub = createLenList(3,LL)
print maxStub
