简体   繁体   English

具有顺序限制的两个列表的元素的排列

[英]Permutations of the elements of two lists with order restrictions

I would like to merge two input lists together and get all permutations of their elements such that the ordering of the elements in each input list is retained. 我想将两个输入列表合并在一起,并获得它们元素的所有排列,以便保留每个输入列表中元素的顺序。

For example, if I have two lists: ['b','a'] and ['c','d'] , I would like to get the following lists: 例如,如果我有两个列表: ['b','a']['c','d'] ,我想获得以下列表:

['b', 'a', 'c', 'd'], 
['b', 'c', 'a', 'd'], 
['b', 'c', 'd', 'a'], 
['c', 'b', 'a', 'd'], 
['c', 'b', 'd', 'a'], 
['c', 'd', 'b', 'a']

where the order of the elements from the original lists is retained (b comes before a, c comes before d). 保留原始列表中元素的顺序(b在a之前,c在d之前)。

I have found a post dealing with a similar problem but using strings as inputs instead of lists: Restricted Permutations of Strings in Python . 我发现了一个处理类似问题的文章,但使用字符串作为输入而不是列表: Python中字符串的受限排列 For example, with the strings "ba" and "cd" as inputs, strings "bacd", "bcad", "bcda", "cbad", "cbda", "cdba" will be returned. 例如,使用字符串“ ba”和“ cd”作为输入,将返回字符串“ bacd”,“ bcad”,“ bcda”,“ cbad”,“ cbda”,“ cdba”。

I have tried to adapt the code there to my problem, but it didn't work. 我尝试将代码修改为我的问题,但是没有用。 Using the same example as above, I got [None, None, None, None, None, None] instead of the 6 lists that I expected. 使用与上述相同的示例,我得到了[无,无,无,无,无,无],而不是我期望的6个列表。 Below is the code I used. 下面是我使用的代码。

def ordered_permutations(list1, list2):
    perms = []
    if len(list1) + len(list2) == 1:
        return [list1 or list2]
    if list1:
        for item in ordered_permutations(list1[1:], list2):
            perms.append([list1[0]].append(item))
    if list2:
        for item in ordered_permutations(list1, list2[1:]):
            perms.append([list2[0]].append(item))
    return perms

list.append() returns None as the list is altered in-place. list.append()返回None因为就地更改了列表。 You should use concatenation instead: 您应该改用串联:

def ordered_permutations(list1, list2):
    perms = []
    if len(list1) + len(list2) == 1:
        return [list1 or list2]
    if list1:
        for item in ordered_permutations(list1[1:], list2):
            perms.append([list1[0]] + item)
    if list2:
        for item in ordered_permutations(list1, list2[1:]):
            perms.append([list2[0]] + item)
    return perms

This then produces your required output: 然后产生所需的输出:

>>> def ordered_permutations(list1, list2):
...     perms = []
...     if len(list1) + len(list2) == 1:
...         return [list1 or list2]
...     if list1:
...         for item in ordered_permutations(list1[1:], list2):
...             perms.append([list1[0]] + item)
...     if list2:
...         for item in ordered_permutations(list1, list2[1:]):
...             perms.append([list2[0]] + item)
...     return perms
... 
>>> for combo in ordered_permutations(['b','a'], ['c', 'd']):
...     print combo
... 
['b', 'a', 'c', 'd']
['b', 'c', 'a', 'd']
['b', 'c', 'd', 'a']
['c', 'b', 'a', 'd']
['c', 'b', 'd', 'a']
['c', 'd', 'b', 'a']

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

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