Python检测字符串是否包含来自特定集的字符的特定长度子字符串

时间:2015-12-26 05:13:14

标签: python string set substring

给定一组字符和长度

s = set('abc')
l = 5

如何确保字符串不包含

之类的子字符串
abcab
aaaaa

长度需要大约为60,所以我不能只生成所有子串。

2 个答案:

答案 0 :(得分:0)

您可以遍历字符串的每个字符,并跟踪s元素的前一个字符数。

def hasSubstring(s, l):
    length = 0
    for c in str:
        if c in s:
            length += 1
        else:
            length = 0
        if length > l:
            return True
    return False

答案 1 :(得分:0)

如何使用productlist-comprehension

from itertools import product

s = set('abc')
l = 5
omit = ['abcab','aaaaa']
def sorter(s,l,omit):
    s= ''.join(list(s))
    unsrted = [''.join(it) for it in list(product(s,repeat=l))]
    filrted = [value for value in unsrted if value not in omit]#just filter here based on the list omit
    return filrted
print sorter(s, l, omit)

输出 -

['aaaac', 'aaaab', 'aaaca', 'aaacc', 'aaacb', 'aaaba', 'aaabc', 'aaabb', 'aacaa', 'aacac', 'aacab', 'aacca', 'aaccc', 'aaccb', 'aacba', 'aacbc', 'aacbb', 'aabaa', 'aabac', 'aabab', 'aabca', 'aabcc', 'aabcb', 'aabba', 'aabbc', 'aabbb', 'acaaa', 'acaac', 'acaab', 'acaca', 'acacc', 'acacb', 'acaba', 'acabc', 'acabb', 'accaa', 'accac', 'accab', 'accca', 'acccc', 'acccb', 'accba', 'accbc', 'accbb', 'acbaa', 'acbac', 'acbab', 'acbca', 'acbcc', 'acbcb', 'acbba', 'acbbc', 'acbbb', 'abaaa', 'abaac', 'abaab', 'abaca', 'abacc', 'abacb', 'ababa', 'ababc', 'ababb', 'abcaa', 'abcac', 'abcca', 'abccc', 'abccb', 'abcba', 'abcbc', 'abcbb', 'abbaa', 'abbac', 'abbab', 'abbca', 'abbcc', 'abbcb', 'abbba', 'abbbc', 'abbbb', 'caaaa', 'caaac', 'caaab', 'caaca', 'caacc', 'caacb', 'caaba', 'caabc', 'caabb', 'cacaa', 'cacac', 'cacab', 'cacca', 'caccc', 'caccb', 'cacba', 'cacbc', 'cacbb', 'cabaa', 'cabac', 'cabab', 'cabca', 'cabcc', 'cabcb', 'cabba', 'cabbc', 'cabbb', 'ccaaa', 'ccaac', 'ccaab', 'ccaca', 'ccacc', 'ccacb', 'ccaba', 'ccabc', 'ccabb', 'cccaa', 'cccac', 'cccab', 'cccca', 'ccccc', 'ccccb', 'cccba', 'cccbc', 'cccbb', 'ccbaa', 'ccbac', 'ccbab', 'ccbca', 'ccbcc', 'ccbcb', 'ccbba', 'ccbbc', 'ccbbb', 'cbaaa', 'cbaac', 'cbaab', 'cbaca', 'cbacc', 'cbacb', 'cbaba', 'cbabc', 'cbabb', 'cbcaa', 'cbcac', 'cbcab', 'cbcca', 'cbccc', 'cbccb', 'cbcba', 'cbcbc', 'cbcbb', 'cbbaa', 'cbbac', 'cbbab', 'cbbca', 'cbbcc', 'cbbcb', 'cbbba', 'cbbbc', 'cbbbb', 'baaaa', 'baaac', 'baaab', 'baaca', 'baacc', 'baacb', 'baaba', 'baabc', 'baabb', 'bacaa', 'bacac', 'bacab', 'bacca', 'baccc', 'baccb', 'bacba', 'bacbc', 'bacbb', 'babaa', 'babac', 'babab', 'babca', 'babcc', 'babcb', 'babba', 'babbc', 'babbb', 'bcaaa', 'bcaac', 'bcaab', 'bcaca', 'bcacc', 'bcacb', 'bcaba', 'bcabc', 'bcabb', 'bccaa', 'bccac', 'bccab', 'bccca', 'bcccc', 'bcccb', 'bccba', 'bccbc', 'bccbb', 'bcbaa', 'bcbac', 'bcbab', 'bcbca', 'bcbcc', 'bcbcb', 'bcbba', 'bcbbc', 'bcbbb', 'bbaaa', 'bbaac', 'bbaab', 'bbaca', 'bbacc', 'bbacb', 'bbaba', 'bbabc', 'bbabb', 'bbcaa', 'bbcac', 'bbcab', 'bbcca', 'bbccc', 'bbccb', 'bbcba', 'bbcbc', 'bbcbb', 'bbbaa', 'bbbac', 'bbbab', 'bbbca', 'bbbcc', 'bbbcb', 'bbbba', 'bbbbc', 'bbbbb']