How many substrings can you make out of a string like abcd
?
How can I get all of its substrings:
['a', 'b', 'c', 'd', 'ab', 'bc', 'cd', 'abc', 'bcd', 'abcd']
How many substrings can you make out of a string like abcd
?
How can I get all of its substrings:
['a', 'b', 'c', 'd', 'ab', 'bc', 'cd', 'abc', 'bcd', 'abcd']
Try this:
def consecutive_groups(iterable):
s = tuple(iterable)
for size in range(1, len(s)+1):
for index in range(len(s)+1-size):
yield iterable[index:index+size]
>>> print list(consecutive_groups('abcd'))
['a', 'b', 'c', 'd', 'ab', 'bc', 'cd', 'abc', 'bcd', 'abcd']
And the number of combinations is simply equal to the sum from 1 to the length of the string, which is equivalent to n * (n + 1) / 2
.
By the way, if you want to avoid duplicates, you can simply use a locally-defined set in the generator function, like so:
def consecutive_groups(iterable):
s = tuple(iterable)
seen = set()
for size in range(1, len(s)+1):
for index in range(len(s)+1-size):
slc = iterable[index:index+size]
if slc not in seen:
seen.add(slc)
yield slc
That code is a little more unwieldy and could probably be optimized for indentation, but it will do for a proof of concept.
Would this do?
import itertools
def substrings(x):
for i, j in itertools.combinations(xrange(len(x)+1), 2):
yield x[i:j]
or as generator expression:
(x[i:j] for i, j in itertools.combinations(xrange(len(x)+1), 2))
The expanded result for your example looks like this:
['a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd']
To order by length, use sort key=len
.
This is what you want:
In [260]: S = 'abcd'
In [261]: list(itertools.chain.from_iterable([list(itertools.combinations(S,i)) for i in range(1,len(S))]))
Out[261]:
[('a',),
('b',),
('c',),
('d',),
('a', 'b'),
('a', 'c'),
('a', 'd'),
('b', 'c'),
('b', 'd'),
('c', 'd'),
('a', 'b', 'c'),
('a', 'b', 'd'),
('a', 'c', 'd'),
('b', 'c', 'd')]
Or if you really want them all as strings, you could do:
In [262]: combos = list(itertools.chain.from_iterable([list(itertools.combinations(S,i)) for i in range(1,len(S))]))
In [263]: [''.join(c) for c in combos]
Out[263]:
['a',
'b',
'c',
'd',
'ab',
'ac',
'ad',
'bc',
'bd',
'cd',
'abc',
'abd',
'acd',
'bcd']
EDIT To get only substrings of S
:
In [270]: list(itertools.chain.from_iterable([[S[i:i+k] for i in range(len(S)-k)] for k in range(1,len(S)+1)])) + [S]
Out[270]: ['a', 'b', 'c', 'ab', 'bc', 'abc', 'abcd']
There are two questions there.
The first, How many substrings can you make out of a string like “abcd”?
is a combinations like this:
import itertools
s='abcd'
com=[list(itertools.combinations(s,x)) for x in range(1,len(s)+1)]
print [''.join(e) for e in sum(com,[])]
prints:
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'bc', 'bd', 'cd', 'abc', 'abd', 'acd', 'bcd', 'abcd']
The second question is how to replicate your example (which is not a 'combination'). You can do that with this code:
>>> [s[i:i+j] for j in range(1,len(s)+1) for i in range(len(s)-j+1)]
['a', 'b', 'c', 'd', 'ab', 'bc', 'cd', 'abc', 'bcd', 'abcd']
I think this works too and while is not the most efficient, it has the attractive of using less complex features.
S = "abcd"
substrings = [S[i:j] for i in range(len(S)) for j in range(i+1,len(S)+1)]
substrings.sort(key=len)
Note however that this approach does not remove identical substrings that might appear. For example if the original substring was "abcdab"
, a
, b
and ab
would appear twice.