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)??

Recommended Answers

All 4 Replies

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

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']
"""

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)

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.