简体   繁体   English

使用列表项的组合创建字符串

[英]create strings using combinations of list items

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.我尝试使用 itertools 来获取列表项的组合,但我没有找到正确的代码来获取我的组合。

Is there an easy way or do I have to combine several itertools.combinations?有没有简单的方法或者我必须结合几个 itertools.combinations? Or do I have to loop over all lists?还是我必须遍历所有列表?

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

Is that what you wanted?那是你想要的吗?

combinations returns n-length combinations without replacement from a single sequence eg all pairs from (0, 1, 2, 3, 4) would be (0, 1), (0, 2), (0, 3), ..., (3, 4). combinations从单个序列返回没有替换的 n 长度组合,例如 (0, 1, 2, 3, 4) 中的所有对将是 (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).这根本不是你想要的,你要找的是itertools.product ,你给它任意数量的可迭代对象,它返回该大小的元组,从每个可迭代对象中选取一个项目(枚举所有可能性)。

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

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).这样做会将其转换为字典,然后返回到列表,删除不需要的重复项(如果有的话)。

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.笛卡尔积的工作方式类似于嵌套 for 循环。 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.即使您使用了两次,您也不希望同一字符串中的分隔符不同,所以我在 f 字符串中重复了它们,而不是在产品中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM