我的问题可能类似于this https://stackoverflow.com/questions/37514193/count-the-number-of-occurrences-of-n-length-not-given-string-in-string-python,却又是另一种情况。
在输入中考虑这个列表:
['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 https://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)