I want to generate all size 2 strings from a list of size 1 string
e.g featureList = gives
I wrote this code

def genC(featureList):
    for i in range(0,len(featureList) - 1,1):
        for k in range(1,len(featureList) - 1,1):
            if i+k <= len(featureList)-1:
                colocn = featureList[i] + featureList[i+k]
                prunedK.append(colocn)
                continue
            continue
    return prunedK

However this does not give me all the required strings.It doesnt give me that 2 length string for which i=0 and k=len(featureList) -1.Any ideas why this is happening(even though the if condition is satisfied)??

Re: Producing list of 2 length strings from list of 1 length strings 80 80

Given featureList = what do you expect as result?
Please give complete result.

Re: Producing list of 2 length strings from list of 1 length strings 80 80

In the absence of additional details, maybe our friend wants this result ...

def genC(featureList):
    prunedK = []
    for i in range(0,len(featureList)):
        for k in range(0,len(featureList)):
            #print i, k
            if i != k:
                colocn = featureList[i] + featureList[k]
                prunedK.append(colocn)
    return prunedK

print genC(['a','b','c','d'])

"""
result =
['ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc']
"""
Re: Producing list of 2 length strings from list of 1 length strings 80 80

actually i want to generate all 2 length pairs...
Given i want to produce
Order of these strings doesnt matter.Also order doesnt matter inside the string e.g 'ab' or 'ba' is the same.
Vegaseat's code gives each result twice and in opposite order.e.g 'bc' and 'cb'.
My code gives n-1 results out of n expected(in this case 'ad' is not outputted)

Re: Producing list of 2 length strings from list of 1 length strings 80 80

It was an error in the boundary condition, corrected it...

def genC(featureList):
    l = ['a','b','c','d']
    prunedK = []
    for i in range(0,len(featureList) - 1,1):
        for k in range(1,len(featureList),1):
            if i+k <= len(featureList) - 1:
                colocn = featureList[i] + featureList[i+k]
                prunedK.append(colocn)
                continue
            continue
    return prunedK
Be a part of the DaniWeb community

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