[EMAIL PROTECTED] wrote: > Hi, > I want to generate all non-empty substrings of a string of length >=2. > Also, > each substring is to be paired with 'string - substring' part and vice > versa. > Thus, ['abc'] gives me [['a', 'bc'], ['bc', 'a'], ['ab', 'c'], ['c', > 'ab'], ['b', 'ac'], ['ac', 'b']] etc. > Similarly, 'abcd' should give me [['a', 'bcd'], ['bcd', 'a'], ['abc', > 'd'], ['d', 'abc'], ['b', 'acd'], ['acd', 'b'],['c', 'abd'], ['abd', 'c'], > ['ab', 'cd'], ['cd', 'ab'], ['bc', 'ad'], ['ad', 'bc'], ['ac', > 'bd'],['bd','ac']] >
from a previous post (http://groups.google.com/group/comp.lang.python/browse_frm/thread/fa40c76544b1c515/cef5b578bb00e61b?lnk=st&q=&rnum=23#cef5b578bb00e61b) def nkRange(n,k): m = n - k + 1 indexer = range(0, k) vector = range(1, k+1) last = range(m, n+1) yield vector while vector != last: high_value = -1 high_index = -1 for i in indexer: val = vector[i] if val > high_value and val < m + i: high_value = val high_index = i for j in range(k - high_index): vector[j+high_index] = high_value + j + 1 yield vector def kSubsets(s, k): for vector in nkRange(len(s),k): yield ''.join( s[i-1] for i in vector ) print list( kSubsets('abcd',2) ) ['ab', 'ac', 'ad', 'bc', 'bd', 'cd'] -- http://mail.python.org/mailman/listinfo/python-list