2

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.

ioCron
  • 873
  • 8
  • 9
  • 5
    Is there a reason why the hyphen is allowed to be used more than once in a single combination? Will the list always contain two non-hypen terms? Will the terms always be combined with a hyphen or could it be other characters? The more clear you are, the more clear the answers will be. – jeremye Sep 02 '17 at 20:39
  • Yes, but maybe i should rephrase my orignal post just a little. I actually have a second list with some special characters (like -,_. etc.), which need to be combined on the start, end and middle of the other string combinations. But due to performance reasons I didn't want to pre-create a huge list of combinations, because I am checking the result against the filesystem (as weird as it sounds) first, and if no valid directory is found, then it goes to the next iteration and starts generating a new set of combinations with the next special character. I hope that doesn't sound too confusing. – ioCron Sep 02 '17 at 21:01
  • ioCron: Yes, you need to [edit] your question and specify what you really want. – martineau Sep 02 '17 at 21:08
  • 1
    Not confusing, it's important information. So you could have a list of three terms, and you would want one of the combinations to be `-one-two-three-` with the character between every term? – jeremye Sep 02 '17 at 21:08
  • Thanks for the feedback, I've updated my post, maybe that clarifies my problem a bit better. – ioCron Sep 02 '17 at 22:19

2 Answers2

1

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']
Jonas Adler
  • 10,365
  • 5
  • 46
  • 73
  • Might want to add an `if x` to the end of the comprehension expression to avoid getting the empty string. – martineau Sep 02 '17 at 20:56
  • Maybe, it depends on what OP wants. I'll wait for some clarification. The hyphen issue is also open – Jonas Adler Sep 02 '17 at 20:59
  • Thank you Jonas, that makes it already easier. I've updated my original post, because it wasn't accurate enough. P.S. *I am still very new to python (I am coming from other programming languages), I've nearly spend half the day on this problem, maybe next time I should try easier tasks first :)* – ioCron Sep 02 '17 at 22:33
0

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)]
Fady Saad
  • 1,169
  • 8
  • 13