繁体   English   中英

Itertools函数生成唯一的排列

[英]Itertools function to generate unique permutations

我想知道是否有itertools方式可以产生以下组合/排列:

list = ['x', 'o']

# when character 'x' is allowed to occupy 1 place with total places of 4:

a = [['o','o','o','x'],
     ['o','o','x','o'],
     ['o','x','o','o'],
     ['x','o','o','o']]

# when character 'x' is allowed to occupy 2 places with total places of 4:

b = [['o','o','x','x'],
     ['o','x','x','o'],
     ['x','x','o','o'],
     ['x','o','x','o'],
     ['o','x','o','x'],
     ['x','o','o','x']]

我想知道是否有办法使用itertools.product或类似的功能来实现这一目标?

itertools.permutations还接受字符串作为参数:

from itertools import permutations
>>> list(permutations("ooox"))
[('o', 'o', 'o', 'x'), ('o', 'o', 'x', 'o'), ('o', 'o', 'o', 'x'), ('o', 'o', 'x', 'o'), ('o', 'x', 'o', 'o'), ('o', 'x', 'o', 'o'), ('o', 'o', 'o', 'x'), ('o', 'o', 'x', 'o'), ('o', 'o', 'o', 'x'), ('o', 'o', 'x', 'o'), ('o', 'x', 'o', 'o'), ('o', 'x', 'o', 'o'), ('o', 'o', 'o', 'x'), ('o', 'o', 'x', 'o'), ('o', 'o', 'o', 'x'), ('o', 'o', 'x', 'o'), ('o', 'x', 'o', 'o'), ('o', 'x', 'o', 'o'), ('x', 'o', 'o', 'o'), ('x', 'o', 'o', 'o'), ('x', 'o', 'o', 'o'), ('x', 'o', 'o', 'o'), ('x', 'o', 'o', 'o'), ('x', 'o', 'o', 'o')]

>>> list(permutations("ooxx"))
[('o', 'o', 'x', 'x'), ('o', 'o', 'x', 'x'), ('o', 'x', 'o', 'x'), ('o', 'x', 'x', 'o'), ('o', 'x', 'o', 'x'), ('o', 'x', 'x', 'o'), ('o', 'o', 'x', 'x'), ('o', 'o', 'x', 'x'), ('o', 'x', 'o', 'x'), ('o', 'x', 'x', 'o'), ('o', 'x', 'o', 'x'), ('o', 'x', 'x', 'o'), ('x', 'o', 'o', 'x'), ('x', 'o', 'x', 'o'), ('x', 'o', 'o', 'x'), ('x', 'o', 'x', 'o'), ('x', 'x', 'o', 'o'), ('x', 'x', 'o', 'o'), ('x', 'o', 'o', 'x'), ('x', 'o', 'x', 'o'), ('x', 'o', 'o', 'x'), ('x', 'o', 'x', 'o'), ('x', 'x', 'o', 'o'), ('x', 'x', 'o', 'o')]

要将它们存储在问题列表中所示的列表列表中,可以使用map(list, permutations("ooox"))

如您在注释部分中提到的,我们可以为该作业编写一个特定的函数,该函数接受所需的输入,但是请注意,当第一个字符串的长度不为1时,此函数将以不太理想的方式运行:

from itertools import permutations
def iterate(lst, length, places):
    return set(permutations(lst[0]*(length-places)+lst[1]*places))

演示:

>>> from pprint import pprint
>>> pprint(iterate(["o","x"], 4, 1))
{('o', 'o', 'o', 'x'),
 ('o', 'o', 'x', 'o'),
 ('o', 'x', 'o', 'o'),
 ('x', 'o', 'o', 'o')}
>>> pprint(iterate(["o","x"], 4, 2))
{('o', 'o', 'x', 'x'),
 ('o', 'x', 'o', 'x'),
 ('o', 'x', 'x', 'o'),
 ('x', 'o', 'o', 'x'),
 ('x', 'o', 'x', 'o'),
 ('x', 'x', 'o', 'o')}

您可以基于itertools.combinations创建自己的函数(或生成器):

from itertools import combinations

def equivalence_permutations(x, o):
    """Create all unique permutations with `x` x'es and `o` o's."""
    total = x+o
    for indices in combinations(range(total), x):
        lst = ['o']*total
        for index in indices:
            lst[index] = 'x'
        yield lst

combinations可确保索引唯一,而无需使用set或任何其他贪婪操作。 因此,在这些情况下,它应该更快。 例如:

>>> list(equivalence_permutations(2, 2))  # 2 x and 2 o
[['x', 'x', 'o', 'o'],
 ['x', 'o', 'x', 'o'],
 ['x', 'o', 'o', 'x'],
 ['o', 'x', 'x', 'o'],
 ['o', 'x', 'o', 'x'],
 ['o', 'o', 'x', 'x']]

>>> list(equivalence_permutations(1, 3))  # 1 x and 3 o
[['x', 'o', 'o', 'o'],
 ['o', 'x', 'o', 'o'],
 ['o', 'o', 'x', 'o'],
 ['o', 'o', 'o', 'x']]

暂无
暂无

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

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