0

I like to create strings in the following manner: The string has a prefix, a main part and a suffix, all separated by a delimiter. The prefixes, suffixes and delimiters are stored in lists

ListOfPrefixes=['pre1','pre2']
ListOfSuffixes=['suf1','suf2','suf3']
ListOfDelims=['-','_','']
MainPart = 'main'

Now I like to create strings using combinations of the list items like

pre1-main-suf1
pre2-main-suf1
pre1-main-suf2
...
pre2_main_suf2
...
pre1mainsuf3

I tried to use itertools to get combinations of the list items, but I didn't find the correct code to get my combinatons.

Is there an easy way or do I have to combine several itertools.combinations? Or do I have to loop over all lists?

3
  • can you add the code you tried Commented Feb 27, 2020 at 7:29
  • I for got to mention one thing: I need strings of the form pre1_pre2_main_suf1_suf2, too. So the combination of of prefixes +main pat+combination of suffixes Commented Feb 27, 2020 at 7:34
  • Now that's a very different problem… Commented Feb 27, 2020 at 7:38

5 Answers 5

2
import itertools
for pre, suf, sep in itertools.product(ListOfPrefixes, ListOfSuffixes, ListOfDelims):
    print(sep.join([pre, MainPart, suf])

Is that what you wanted?

Sign up to request clarification or add additional context in comments.

2 Comments

Thats it: if I define .product(ListOfPrefixex,ListOfPrefixes,ListOfSuffixes,ListOfSuffixes,ListOfDelims) I get all my needed combinations!
In this case, of course, you need to change to pre1, pre2, suf1, suf2 accordingly.
1

combinations returns n-length combinations without replacement from a single sequence e.g. all pairs from (0, 1, 2, 3, 4) would be (0, 1), (0, 2), (0, 3), ..., (3, 4).

That's not what you want at all here, what you're looking for is itertools.product, you give it any number of iterables and it returns tuples of that size with one item picked from each iterable (enumerating all possibilities).

def the_thing(main_part, prefixes, delimiters, suffixes):
    for pre, sep, suf in product(prefixes, delimiters, suffixes):
        print(sep.join([pre, main_part, suf])

1 Comment

I like the sep.join part, very nice.
0

You want a cartesian product.

from itertools import product

[f"{p}{d}{MainPart}{d}{s}" for d, s, p in
 product(ListOfDelims, ListOfSuffixes, ListOfPrefixes)]

Pretty-printed this is

['pre1-main-suf1',
 'pre2-main-suf1',
 'pre1-main-suf2',
 'pre2-main-suf2',
 'pre1-main-suf3',
 'pre2-main-suf3',
 'pre1_main_suf1',
 'pre2_main_suf1',
 'pre1_main_suf2',
 'pre2_main_suf2',
 'pre1_main_suf3',
 'pre2_main_suf3',
 'pre1mainsuf1',
 'pre2mainsuf1',
 'pre1mainsuf2',
 'pre2mainsuf2',
 'pre1mainsuf3',
 'pre2mainsuf3']

A Cartesian product works like nested for loops. You didn't want the delimiters in the same string to differ, even though you used them twice, so I repeated them in the f-string, not in the product.

Comments

0
import itertools

ListOfPrefixes=['pre1','pre2']
MainPart = ['main']
ListOfSuffixes=['suf1','suf2','suf3']
ListOfDelims=['-','_','']


FullList = [ListOfPrefixes, MainPart, ListOfSuffixes]

for x in list(itertools.product(*FullList)):
    for i in range(len(ListOfDelims)):
        print(ListOfDelims[i].join(x))

output:

pre1-main-suf1
pre1_main_suf1
pre1mainsuf1
pre1-main-suf2
pre1_main_suf2
pre1mainsuf2
pre1-main-suf3
pre1_main_suf3
pre1mainsuf3
pre2-main-suf1
pre2_main_suf1
pre2mainsuf1
pre2-main-suf2
pre2_main_suf2
pre2mainsuf2
pre2-main-suf3
pre2_main_suf3
pre2mainsuf3

Comments

0

You can use a nested loop and iterate through each of the prefixes, suffixes and delimiters then concatenate like this.

EDIT: if you want the extra combinations from your updated comment, you can just add more nests to the loop and add a condition to check if the first and second prefix or first and second suffix are the same if you dont want them:

ListOfPrefixes=['pre1','pre2']
ListOfSuffixes=['suf1','suf2','suf3']
ListOfDelims=['-','_','']
MainPart = 'main'


for prefix in ListOfPrefixes:
    for suffix in ListOfSuffixes:
        for delimiter in ListOfDelims:
            for prefix2 in ListOfPrefixes:
                for suffix2 in ListOfSuffixes:
                    if prefix != prefix2 and suffix != suffix2:
                        print(prefix + delimiter + prefix2 + delimiter + MainPart + delimiter + suffix + delimiter + suffix2)

#and a second nested loop for the rest:

for prefix in ListOfPrefixes:
    for suffix in ListOfSuffixes:
        for delimiter in ListOfDelims:
            print(prefix + delimiter + MainPart + delimiter + suffix)

Output:

pre1-pre2-main-suf1-suf2
pre1-pre2-main-suf1-suf3
pre1_pre2_main_suf1_suf2
pre1_pre2_main_suf1_suf3
pre1pre2mainsuf1suf2
pre1pre2mainsuf1suf3
pre1-pre2-main-suf2-suf1
pre1-pre2-main-suf2-suf3
pre1_pre2_main_suf2_suf1
pre1_pre2_main_suf2_suf3
pre1pre2mainsuf2suf1
pre1pre2mainsuf2suf3
pre1-pre2-main-suf3-suf1
pre1-pre2-main-suf3-suf2
pre1_pre2_main_suf3_suf1
pre1_pre2_main_suf3_suf2
pre1pre2mainsuf3suf1
pre1pre2mainsuf3suf2
pre2-pre1-main-suf1-suf2
pre2-pre1-main-suf1-suf3
pre2_pre1_main_suf1_suf2
pre2_pre1_main_suf1_suf3
pre2pre1mainsuf1suf2
pre2pre1mainsuf1suf3
pre2-pre1-main-suf2-suf1
pre2-pre1-main-suf2-suf3
pre2_pre1_main_suf2_suf1
pre2_pre1_main_suf2_suf3
pre2pre1mainsuf2suf1
pre2pre1mainsuf2suf3
pre2-pre1-main-suf3-suf1
pre2-pre1-main-suf3-suf2
pre2_pre1_main_suf3_suf1
pre2_pre1_main_suf3_suf2
pre2pre1mainsuf3suf1
pre2pre1mainsuf3suf2
pre1-main-suf1
pre1_main_suf1
pre1mainsuf1
pre1-main-suf2
pre1_main_suf2
pre1mainsuf2
pre1-main-suf3
pre1_main_suf3
pre1mainsuf3
pre2-main-suf1
pre2_main_suf1
pre2mainsuf1
pre2-main-suf2
pre2_main_suf2
pre2mainsuf2
pre2-main-suf3
pre2_main_suf3
pre2mainsuf3

Storing the combinations in a list is useful when doing this method. If you have redundant data within the list of combinations you can use this trick:

combinationList = list(dict.fromkeys(combinationList))

Doing this will convert it to a dictionary then back to a list, removing the unwanted duplicates (if u have any).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.