简体   繁体   English

itertools and permutations - 合并两个列表以进行所有可能的组合

[英]itertools and permutations - Merge two lists in order to make all possible combinations

I have two lists: a and b .我有两个列表: ab a is a list with three or more strings, while b is a list of separators. a是包含三个或更多字符串的列表,而b是分隔符列表。

I need to generate all the possible combinations of a and than merge the result with all the possible combinations of b (See the example for better understanding).我需要生成,比合并与B的(参见更好地理解的例子)所有可能的组合结果中的所有可能的组合。

I want all three items from list a to be in each line of the result, in different order (in order to cover or the possible combinations).我希望列表a 中的所有三个项目都在结果的每一行中,以不同的顺序(为了覆盖或可能的组合)。 Items from list a can not be repeated, while items of list b can be repeated.列表a 中的项目不能重复,而列表b 中的项目可以重复。

I have no idea of how to make it works, so your help would be so appreciated.我不知道如何使它工作,所以你的帮助将不胜感激。

Thanks!谢谢!

a = ["test1", "test2", "test3"]
b = ["_", "-", ".", ""]

# Expected result
test1_test2_test3
test2_test1_test3

. . . .

test1_test2-test3
test1_test2.test3

. . . .

test3test2_test1

. . . .

Edit:编辑:

from itertools import permutations, combinations, product, chain

a = ["test1", "test2", "test3"]
b = ["_", "-", ".", ""]

a_permutations = permutations(a)
b_permutations = product(b, repeat=len(a)-1)

for ap in a_permutations:
    for bp in b_permutations:
        result = ''.join([''.join(word) for word in zip(ap, bp)]) + ap[-1]
        print result

For some reason it doesn't work properly..由于某种原因它不能正常工作..

Since you show no code of your own, I'll just give you some ideas.由于您没有展示自己的代码,我只会给您一些想法。 If you want more details, edit your question to add some more of your own work then ask for details.如果您需要更多详细信息,请编辑您的问题以添加更多您自己的工作,然后询问详细信息。

As I look at your desired list it seems that you want the 3 items from list a , without repetition, and two items from list b , perhaps with repetition, and to interleave the items from the two lists.当我查看您想要的列表时,您似乎希望列表a的 3 个项目不重复,以及列表b两个项目(可能重复),并将两个列表中的项目交错。 I will also assume that the number of items from list b is one less than the length of the given list a .我还将假设列表b的项目数比给定列表a的长度少a (If those assumptions are not what you want, please state that clearly in your question.) (如果这些假设不是您想要的,请在您的问题中明确说明。)

"Combinations" with different orders without repetition are called permutations.具有不同顺序而不重复的“组合”称为排列。 You can get all the permutations of list a with the expression您可以使用表达式获得列表a所有排列

itertools.permutations(a)

and thus all possible collections of items from list b with repetitions with因此列表b所有可能的项目集合与重复

itertools.product(b, repeat=len(a)-1)

For each pair of items from each collection, you can interleave those items by using a zip() operation on the two collections.对于每个集合中的每对项目,您可以通过对两个集合使用zip()操作来交错这些项目。 Since the first collection is longer by 1 than the second, that final item will be left out, but you can just add that on to the result.由于第一个集合比第二个集合长 1,因此最后一项将被排除在外,但您可以将其添加到结果中。

I could show you a one-liner that does that interleaving but I should leave something for you to do.我可以给你看一个单线,可以进行这种交错,但我应该留下一些东西让你做。 Use those ideas to try your own code.使用这些想法来尝试您自己的代码。 If you do not completely succeed, show us an attempt and I will be glad to show you some final code.如果您没有完全成功,请向我们展示一个尝试,我将很高兴向您展示一些最终代码。

Note that following my ideas will result in a list in a different order than what you show.请注意,遵循我的想法将导致列表的顺序与您显示的顺序不同。 The list I would get would start with我会得到的清单将从

test1_test2_test3
test1_test3_test2
test2_test1_test3
test2_test3_test1
test3_test1_test2
test3_test2_test1
test1_test2-test3
test1_test3-test2
test2_test1-test3
test2_test3-test1
test3_test1-test2
test3_test2-test1
test1_test2.test3

and so on.等等。 This order fixes the separators then permutes the items from list a .此顺序固定分隔符,然后排列列表a的项目。 You could do it the other way round, of course, and get a different order.当然,你可以反过来做,然后得到不同的顺序。


Now that you have shown some work of your own, here is my code.现在你已经展示了你自己的一些作品,这是我的代码。 This assumes that lists a and b have no repetitions.这假设列表ab没有重复。 This also does a trick to simplify the handling of the problem that there is one more item than separator--I just add the empty separator "" to the end of a tuple of separators.这也有助于简化问题的处理,即比分隔符多一个项目——我只是将空分隔符""添加到分隔符元组的末尾。 The two lengths (of items and of separators) are then equal and the empty separator at the end of each string does nothing.然后两个长度(项目和分隔符)相等,每个字符串末尾的空分隔符什么也不做。

import itertools

a = ["test1", "test2", "test3"]
b = ["_", "-", ".", ""]

for separators in itertools.product(b, repeat=len(a) - 1):
    for items in itertools.permutations(a):
        print("".join(v for pair in zip(items, separators + ("",)) for v in pair))

This code gives the printout此代码提供打印输出

test1_test2_test3
test1_test3_test2
test2_test1_test3
test2_test3_test1
test3_test1_test2
test3_test2_test1
test1_test2-test3
test1_test3-test2
test2_test1-test3
test2_test3-test1
test3_test1-test2
test3_test2-test1
test1_test2.test3
test1_test3.test2
test2_test1.test3
test2_test3.test1
test3_test1.test2
test3_test2.test1
test1_test2test3
test1_test3test2
test2_test1test3
test2_test3test1
test3_test1test2
test3_test2test1
test1-test2_test3
test1-test3_test2
test2-test1_test3
test2-test3_test1
test3-test1_test2
test3-test2_test1
test1-test2-test3
test1-test3-test2
test2-test1-test3
test2-test3-test1
test3-test1-test2
test3-test2-test1
test1-test2.test3
test1-test3.test2
test2-test1.test3
test2-test3.test1
test3-test1.test2
test3-test2.test1
test1-test2test3
test1-test3test2
test2-test1test3
test2-test3test1
test3-test1test2
test3-test2test1
test1.test2_test3
test1.test3_test2
test2.test1_test3
test2.test3_test1
test3.test1_test2
test3.test2_test1
test1.test2-test3
test1.test3-test2
test2.test1-test3
test2.test3-test1
test3.test1-test2
test3.test2-test1
test1.test2.test3
test1.test3.test2
test2.test1.test3
test2.test3.test1
test3.test1.test2
test3.test2.test1
test1.test2test3
test1.test3test2
test2.test1test3
test2.test3test1
test3.test1test2
test3.test2test1
test1test2_test3
test1test3_test2
test2test1_test3
test2test3_test1
test3test1_test2
test3test2_test1
test1test2-test3
test1test3-test2
test2test1-test3
test2test3-test1
test3test1-test2
test3test2-test1
test1test2.test3
test1test3.test2
test2test1.test3
test2test3.test1
test3test1.test2
test3test2.test1
test1test2test3
test1test3test2
test2test1test3
test2test3test1
test3test1test2
test3test2test1

This can be achived in the pythonic way using permutations , product and combinations module from itertools below is the working snippet:这可以使用来自itertools permutationsproductcombinations模块以pythonic方式实现,下面是工作片段:

from itertools import permutations, combinations, product

a = ["test1", "test2", "test3"]
b = ["_", "-", ".", ""]

for com in combinations(b, len(a) - 1):
    for per in product(com, repeat=len(a) - 1):
        for ear_per in permutations(a):
            out = ''.join(map(''.join, zip(list(ear_per[:-1]), per)))
            print(out + list(ear_per)[-1])

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

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