Python itertools permutations how to include repeating characters [duplicate]

隐身守侯 提交于 2019-11-29 09:26:59

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")

What you seem to be looking for is a Cartesian product, not a permutation, which is also provided by itertools.

You might do well to familiarize yourself with the differences between permutation, combination, combination with replacement, and Cartesian product to decide what works best your application, but chances are, you're looking for another of the options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!