2 个字符的 x 长度的排列 [英] Permutation of x length of 2 characters

查看:40
本文介绍了2 个字符的 x 长度的排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个字符:a &b.我想获得 a<的所有可能的 排列 的列表/code> 和 b 对于给定的长度(即 5),使用 Python 2.7.

Lets say I have two characters: a & b. I want to get a list of all possible permutations of a and b for a given length (i.e. 5), using Python 2.7.

我看过itertools.permutations,但是无法找到允许我使用 2 个字符来构建超过 2 个字符的排列的方法.

I have looked at itertools.permutations, but was unable to find a way that allowed me to use 2 characters to build a permutation longer than 2 characters.

我还查看了 Python 排列的多个 SO 答案,例如这个,但是这个,以及其他的,只会给出 len(input) 长度的排列.

I have also looked at multiple SO answers for Python permutations, such as this one, but this, along with the others, will only give permutations of len(input) length.

另外,如果有使用不同编程语言的好方法,请提出建议.我并没有一成不变地为此使用 Python.

Also, if there is a good way to do this using a different programming language, please suggest it. I am not set in stone on using Python for this.

推荐答案

那样:

import itertools
x = ['a','b']
print [p for p in itertools.product(x, repeat=5)]

这篇关于2 个字符的 x 长度的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆