向组合和排列添加文本

问题描述 投票:0回答:1

我是编码新手。我有一个问题。如何将文本添加到我创建的组合和排列中?另外,有没有一种方法可以删除重复排列中的重复状态,即像这样打印?如果可以的话,是怎么做到的?

colour = list(itertools.permutations(["green","green","green","yellow","yellow"],5))
print("first one is " + colour[0] + " and second one is " + colour[1] + " and third one is " + colour[2] + " and fourth one is " + colour[3] + " and fifth one is " + colour[4])

我写了这样的代码,但它不起作用。在前五个元素的排列开始时,我想添加文本,例如第一个到第一行,第二个到第二行,我该怎么做?我希望我能够解释一下。如果您能提供帮助,我会很高兴。

python combinations permutation
1个回答
1
投票

我不确定我知道你在问什么,但要获得多个相同项目的不同排列,你可以使用 from sympy.utilities.iterables import multiset_permutations。

from sympy.utilities.iterables import multiset_permutations

colour = list(multiset_permutations(["green","green","green","yellow","yellow"],5))

for perm in colour:
    print(perm)

打印:

['green', 'green', 'green', 'yellow', 'yellow']
['green', 'green', 'yellow', 'green', 'yellow']
['green', 'green', 'yellow', 'yellow', 'green']
['green', 'yellow', 'green', 'green', 'yellow']
['green', 'yellow', 'green', 'yellow', 'green']
['green', 'yellow', 'yellow', 'green', 'green']
['yellow', 'green', 'green', 'green', 'yellow']
['yellow', 'green', 'green', 'yellow', 'green']
['yellow', 'green', 'yellow', 'green', 'green']
['yellow', 'yellow', 'green', 'green', 'green']
© www.soinside.com 2019 - 2024. All rights reserved.