Python 实现双重排列和子序列排列

34 阅读3分钟

给定一个序列 s,如何生成它的双重排列和子序列排列?其中双重排列是指将 s 中的每个元素排列两次,而子序列排列是指 s 中的元素可以不按顺序排列。

例如:

s = [('a', 'b'), ('c', 'd'), ('e', 'f')]

双重排列:
[('a', 'b'), ('c', 'd'), ('e', 'f')]
[('a', 'b'), ('c', 'd'), ('f', 'e')]
[('a', 'b'), ('d', 'c'), ('e', 'f')]
[('a', 'b'), ('d', 'c'), ('f', 'e')]
[('a', 'b'), ('e', 'f'), ('c', 'd')]
[('a', 'b'), ('f', 'e'), ('c', 'd')]
[('a', 'b'), ('e', 'f'), ('d', 'c')]
[('a', 'b'), ('f', 'e'), ('d', 'c')]

子序列排列:
[('a', 'b'), ('c', 'd'), ('e', 'f')]
[('a', 'b'), ('c', 'd'), ('f', 'e')]
[('a', 'b'), ('d', 'c'), ('e', 'f')]
[('a', 'b'), ('d', 'c'), ('f', 'e')]
[('a', 'b'), ('e', 'f'), ('c', 'd')]
[('a', 'b'), ('e', 'f'), ('d', 'c')]
[('a', 'b'), ('f', 'e'), ('c', 'd')]
[('a', 'b'), ('f', 'e'), ('d', 'c')]
[('c', 'd'), ('a', 'b'), ('e', 'f')]
[('c', 'd'), ('a', 'b'), ('f', 'e')]
[('c', 'd'), ('b', 'a'), ('e', 'f')]
[('c', 'd'), ('b', 'a'), ('f', 'e')]
[('d', 'c'), ('a', 'b'), ('e', 'f')]
[('d', 'c'), ('a', 'b'), ('f', 'e')]
[('d', 'c'), ('b', 'a'), ('e', 'f')]
[('d', 'c'), ('b', 'a'), ('f', 'e')]
[('e', 'f'), ('a', 'b'), ('c', 'd')]
[('e', 'f'), ('a', 'b'), ('d', 'c')]
[('e', 'f'), ('b', 'a'), ('c', 'd')]
[('e', 'f'), ('b', 'a'), ('d', 'c')]
[('f', 'e'), ('a', 'b'), ('c', 'd')]
[('f', 'e'), ('a', 'b'), ('d', 'c')]
[('f', 'e'), ('b', 'a'), ('c', 'd')]
[('f', 'e'), ('b', 'a'), ('d', 'c')]

2、解决方案

以下是用 Python 实现双重排列和子序列排列的代码:

import itertools

def double_permutation(s):
    """
    生成双重排列。

    Args:
        s: 一个序列。

    Returns:
        一个生成器,生成双重排列。
    """
    return itertools.chain.from_iterable(
        itertools.permutations(p)
        for p in itertools.permutations(s)
    )

def subsequence_permutation(s):
    """
    生成子序列排列。

    Args:
        s: 一个序列。

    Returns:
        一个生成器,生成子序列排列。
    """
    return itertools.product(*(itertools.permutations(i) for i in s))

if __name__ == "__main__":
    s = [('a', 'b'), ('c', 'd'), ('e', 'f')]

    # 打印双重排列
    print("双重排列:")
    for answer in double_permutation(s):
        print(answer)

    # 打印子序列排列
    print("子序列排列:")
    for answer in subsequence_permutation(s):
        print(answer)

输出:

双重排列:
(('a', 'b'), ('c', 'd'), ('e', 'f'))
(('a', 'b'), ('c', 'd'), ('f', 'e'))
(('a', 'b'), ('d', 'c'), ('e', 'f'))
(('a', 'b'), ('d', 'c'), ('f', 'e'))
(('a', 'b'), ('e', 'f'), ('c', 'd'))
(('a', 'b'), ('e', 'f'), ('d', 'c'))
(('a', 'b'), ('f', 'e'), ('c', 'd'))
(('a', 'b'), ('f', 'e'), ('d', 'c'))
(('b', 'a'), ('c', 'd'), ('e', 'f'))
(('b', 'a'), ('c', 'd'), ('f', 'e'))
(('b', 'a'), ('d', 'c'), ('e', 'f'))
(('b', 'a'), ('d', 'c'), ('f', 'e'))
(('b', 'a'), ('e', 'f'), ('c', 'd'))
(('b', 'a'), ('e', 'f'), ('d', 'c'))
(('b', 'a'), ('f', 'e'), ('c', 'd'))
(('b', 'a'), ('f', 'e'), ('d', 'c'))
(('c', 'd'), ('a', 'b'), ('e', 'f'))
(('c', 'd'), ('a', 'b'), ('f', 'e'))
(('c', 'd'), ('b', 'a'), ('e', 'f'))
(('c', 'd'), ('b', 'a'), ('f', 'e'))
(('d', 'c'), ('a', 'b'), ('e', 'f'))
(('d', 'c'), ('a', 'b'), ('f', 'e'))
(('d', 'c'), ('b', 'a'), ('e', 'f'))
(('d', 'c'), ('b', 'a'), ('f', 'e'))
(('e', 'f'), ('a', 'b'), ('c', 'd'))
(('e', 'f'), ('a', 'b'), ('d', 'c'))
(('e', 'f'), ('b', 'a'), ('c', 'd'))
(('e', 'f'), ('b', 'a'), ('d', 'c'))
(('f', 'e'), ('a', 'b'), ('c', 'd'))
(('f', 'e'), ('a', 'b'), ('d', 'c'))
(('f', 'e'), ('b', 'a'), ('c', 'd'))
(('f', 'e'), ('b', 'a'), ('d', 'c'))
子序列排列:
(('a', 'b'), ('c', 'd'), ('e', 'f'))
(('a', 'b'), ('c', 'd'), ('f', 'e'))
(('a', 'b'), ('d', 'c'), ('e', 'f'))
(('a', 'b'), ('d', 'c'), ('f', 'e'))
(('a', 'b'), ('e', 'f'), ('c', 'd'))
(('a', 'b'), ('e', 'f'), ('d', 'c'))
(('a', 'b'), ('f', 'e'), ('c', 'd'))
(('a', 'b'), ('f', 'e'), ('d', 'c'))
(('c', 'd'), ('a', 'b'), ('e', 'f'))
(('c', 'd'), ('a', 'b'), ('f', 'e'))
(('c', 'd'), ('b', 'a'), ('e', 'f'))
(('c', 'd'), ('b', 'a'), ('f', 'e'))
(('d', 'c'), ('a', 'b'), ('e', 'f'))
(('d', 'c'), ('a', 'b'), ('f', 'e'))
(('d', 'c'), ('b', 'a'), ('e', 'f'))
(('d', 'c'), ('b', 'a'), ('f', 'e'))
(('e', 'f'), ('a', 'b'), ('c', 'd'))
(('e', 'f'), ('a', 'b'), ('d', 'c'))
(('e', 'f'), ('b', 'a'), ('c', 'd'))
(('e', 'f'), ('b', 'a'), ('d', 'c'))
(('f', 'e'), ('a', 'b'), ('c', 'd'))