本文介绍了Python IterTools排列如何包括重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Python Itertools.permutations(),我想接收和输出具有重复字符的排列.例如,下面是我的函数及其当前输出.
With Python Itertools.permutations() I would like to receive and output of permutations with repeating characters. For an example this my function below and its current output.
def perm(n,i):
b = 0
while b < n:
n= n -1
from itertools import permutations as p
file.write('\n'.join([''.join(item) for item in p(i,n)]))
perm(4,'0123')
输出为:
012
013
021
023
031
032
102
103
120
123
130
132
201
203
210
213
230
231
301
302
310
312
320
321.....
我将如何获得类似112或222的输出?
how would I get an output like 112 or 222?
组合在排列位置不是特定于顺序的.我要寻找的是找到所有组合,然后是每个组合的每个排列.这可能吗?
from what I understand combinations are not order specific where permutations are. what I am looking for is finding all combinations then every permutation for each combination. Is this possible?
推荐答案
您根本不需要排列.您想要笛卡尔积:
You don't want permutations at all. You want the cartesian product:
import itertools
def perm(n, seq):
for p in itertools.product(seq, repeat=n):
file.write("".join(p))
file.write("\n")
perm(4, "0123")
这篇关于Python IterTools排列如何包括重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!