Reputation: 57
Suppose, I have a string name = 'baceb'
I want to store its substrings into a list.
For baceb
the substrings will be like - "b, ba, bac, bace, a, ac, ace, aceb, c, ce, ceb, e, eb, baceb"
How can I get this substrings easily?
Upvotes: 1
Views: 67
Reputation: 147166
You can generate a list of all the substrings with a simple nested list comprehension:
s = 'baceb'
subs = [s[i:j+1] for i in range(len(s)) for j in range(i,len(s))]
This will however give you a repeated value, b
, which occurs as both the first and last substring:
['b', 'ba', 'bac', 'bace', 'baceb', 'a', 'ac', 'ace', 'aceb', 'c', 'ce', 'ceb', 'e', 'eb', 'b']
If you don't want any duplicates, and you don't care about ordering, use a set comprehension instead and then convert to a list:
subs = list({ s[i:j+1] for i in range(len(s)) for j in range(i,len(s)) })
Output:
['e', 'ba', 'a', 'aceb', 'c', 'ceb', 'eb', 'bac', 'baceb', 'bace', 'ce', 'ac', 'b', 'ace']
If you do care about ordering, there are many good solutions here that describe how to remove duplicates from a list while preserving ordering.
Upvotes: 3
Reputation: 11342
Try this:
import itertools
s = 'baceb'
lst = [[s[i:j+1] for j in range(i,len(s))] for i in range(len(s))]
ss = set(itertools.chain.from_iterable(lst))
print(ss)
Output
{'e', 'ba', 'a', 'aceb', 'b', 'ace', 'bac', 'bace', 'ce', 'c', 'baceb', 'ceb', 'eb', 'ac'}
Upvotes: 0