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

Dani AI

Generated

A short, practical note: the goal is unordered 2-item combinations (so ab and ba are the same and should appear only once). The usual causes of errors seen above are an off-by-one in the loop ranges (which caused the missing ad in 's original loop) or using two full-range loops with an if i != j check (which produces both orders, as showed). The simplest, most readable options are either a small index-based loop that always uses j > i, or Python's itertools.combinations.

Example using index ranges:

def pairs_joined(lst):
    result = []
    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            result.append(lst[i] + lst[j])
    return result

Example using itertools (concise and idiomatic):

from itertools import combinations

def pairs_with_itertools(lst):
    return [''.join(pair) for pair in combinations(lst, 2)]

Notes and cautions: if the input can contain duplicate elements and you want unique pairs by value (not by position), convert to a canonical form (for example sort the two elements or use set(lst) first) or deduplicate the final results (using a set). Also remember the output size is n*(n-1)/2, so for large lists prefer a generator that yields pairs rather than building a giant list. For reference on combinations see the Python docs: itertools.combinations.

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.