How to generate all k-1 level substrings of a string?

K.S.Sreeram sreeram at tachyontech.net
Mon Jun 5 23:07:27 EDT 2006


Girish Sahani wrote:
> I want to generate all substrings of size k-1 from a string of size k.
> e.g 'abcd' should give me ['abc','abd','bcd','acd']

def get_sub_set( s ) :
   return [s[:i]+s[i+1:] for i in range(len(s))]

>>> print get_sub_set( 'abcd' )
['bcd', 'acd', 'abd', 'abc']

Regards
Sreeram

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20060606/a7a6f98c/attachment.sig>


More information about the Python-list mailing list