I want to generate 'substrings' from a string as follows:
for each substring j of string,generate the 2 lists [j,string - j] and [string -j,j]
e.g If i have a string 'abc' i want to generate the list [['a','bc'],['bc','a'],['c':'ab'],['ab','c'],['b':ac],['ac':b]]
Now i tried the following but its not giving me the desired output :( ....can anyone help??

def gensubset(colocation):
    rulesList= [ ]
    length = len(colocation)
    for i in range(0,length,1):
        rule1 = [colocation[i:i+1],colocation[i+1:length]]
        rule2 = [colocation[i+1:length],colocation[i:i+1]]
        rulesList.append([rule1])
        rulesList.append([rule2)
        continue
    return rulesList

You want 'abc' to generate a list [['a','bc'],['bc','a'],['c','ab'],['ab','c'],['b','ac'],['ac','b']], right?

The way your rules are set up you would never generate a 'ac' or 'ab' element. You got to work on that.

yes,thats what i want.
I am not able to figure out why i cant generate 'ab' and 'ac' type strings (please see what i have written in the bracket in my first post)..shouldnt string[i:j] give me the full substring instead of the substring divided into single elements??

I started playing with code from you and came to this conclusion:

# want 'abc' to generate a list [['a','bc'],['bc','a'],['c','ab'],['ab','c'],['b','ac'],['ac','b']]

def gensubset(colocation):
    rulesList= []
    length = len(colocation)
    for i in range(0,length):
        rule1 = [colocation[i:i+1],colocation[i+1:length]]
        rule2 = [colocation[i+1:length],colocation[i:i+1]]
        rulesList.append(rule1)
        rulesList.append(rule2)
    return rulesList

colocation = 'abc'
sublist = gensubset(colocation)
# not quite there yet, also when i+1 exceeds index you get empty elements ''
print sublist  # [['a', 'bc'], ['bc', 'a'], ['b', 'c'], ['c', 'b'], ['c', ''], ['', 'c']]

To use slicing to create for example 'ab' or 'ac' you need to add more to rules you use:

str1 = 'abc'
length = len(str1)
i = 0
print str1[i:length-1]                # 'ab'
print str1[i:i+1] + str1[i+2:length]  # 'ac'

I don't see these type of slices in rules you have written.

I used another index j...now my code is

def genSubset(colocation):
    rulesList= [ ]
    length = len(colocation)
    i=0
    for j in range(1,length,1):
        rule1 = [colocation[i:i+j],colocation[i+j:length]]
        rule2 = [colocation[i+j:length],colocation[i:i+j]]
        rulesList.append(rule1)
        rulesList.append(rule2)
        continue
    return rulesList

this gives me the following output for genSubset('abc'):
[['a', 'bc'], ['bc', 'a'], ['ab', 'c'], ['c', 'ab']]
the other elements and are not here....in practise these strings could be of any length...
I have found that if i generate this list for all cyclic permutations,i get the required list...so the problem finally boils down to this:how to generate cyclic permutations of a string..e.g 'abcd' should give me abcd,bcda etc

for getting cyclic permutn,we can do:

string = 'abcd'
stringNew = string + string[0]
stringNew = stringNew[1:len(stringNew)]

solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.