ioCron
ioCron

Reputation: 963

How to get combinations of strings in a list with different positioning?

I have a list like:

mylist = [ "abc", "def", "-" ]

I have tried and searched a vast amount of combinations, but nothing that comes close to it, the expected result should look similar to this (a combination of strings that are combined on different positions):

[ "abc", "-abc", "abc-", "-abc-", "abcdef", "-abcdef", "abcdef-", 
"-abcdef-", "abc-def", "-abc-def-", "-abc-def", "abc-def-", "defabc",
"-defabc", "defabc-", "def-abc", "-def-abc", "def-abc-" ]

UPDATE:

The example above is simplified, normally there are 2 lists. One where the regular strings are (abc, def, ...) and the other one contains the special characters (.-_, etc.), (+probably the resultList). E.g.:

regularList =  [ "abc", "def", "ghi", ... ]
specialList = [ ".", "-" ,"_" , ",", ... ]
resultList = [ "abc", "def", "abc-", "-abc", "-abc-", ..., "-abc_def-", ... ]

The Strings ".", "-", "_", ... (specialList) should never be in the end-result as single strings (e.g. [ "-", ".", "abc", "def", "abcdef", ... ])

I need to combine both lists, but with the special exception (as mentioned in my original post) that the characters of the "specialList" need to be at the start, end and middle of the regular string combinations.

The end-result (as mentioned before) would normally look something like this:

resultList = [ "abc", "def", "abc-", "-abc", "-abc-", "abc_", 
"_abc", "_abc_", "_abc-", "-abc_", ..., 
"-abc_def-", "_abc-def_", "abc-def_", ... ]

Side Note:
Not sure if that takes too much processing power / cpu. Thats why I tried to iterate through the "specialList" step-by-step to get the first combinations (then verify it on the filesystem and iterate through the next one) as mentioned in my orig. post / comment, so I removed the "specialList" to simplify the problem in the orig. post, but then I realized that combinations like "-abc_dev-" won't be possible this way, so my original approach / post won't work as I thought anyway.

Upvotes: 2

Views: 139

Answers (2)

Fady Saad
Fady Saad

Reputation: 1189

What you want is called Cartesian Product, You ca achieve it by itertool module with product() function like:

import itertools

mylist = [ "abc", "def", "-" ]

[p for p in itertools.product(mylist, repeat=3)]

Upvotes: 0

Jonas Adler
Jonas Adler

Reputation: 10799

Except for the issue of the number of hyphens (-) being seemily open in your question, you can do this by joining all possible permutations of all possible subsets, specifically:

from itertools import chain, combinations, permutations

def powerset(s):
    # https://stackoverflow.com/questions/7988695/getting-the-subsets-of-a-set-in-python
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

>>> mylist = [ "abc", "def", "-" ]
>>> [''.join(x) for subset in powerset(mylist) for x in permutations(subset)]
['',
 'abc',
 'def',
 '-',
 'abcdef',
 'defabc',
 'abc-',
 '-abc',
 'def-',
 '-def',
 'abcdef-',
 'abc-def',
 'defabc-',
 'def-abc',
 '-abcdef',
 '-defabc']

Upvotes: 1

Related Questions