给定N个项目的列表,如何生成长度LEN的排列? [英] How do I generate permutations of length LEN given a list of N Items?

查看:94
本文介绍了给定N个项目的列表,如何生成长度LEN的排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我正在使用python.

Note: I'm working in python on this.

例如,给定一个列表:

list = ['a','b','c','d','e','f','g','h','i','j']

我想生成具有所有可能的3个项目组合的列表列表:

I want to generate a list of lists with all possible 3-item combinations:

['a','b','c'],
['a','b','d'],
['a','b','e']

排列不应在排列中使用两次相同的项目,但顺序很重要,并且代表应包含的不同排列,例如,

The permutations should not use the same item twice in a permutation, but the order is important and represents distinct permutations that should be included, e.g.,

['a','b','c'],
['a','c','b']

应该都包括在内.

"3"是我要生成的置换的魔术长度,但是我不会轻视任意长度置换的解决方案.

"3" is the magic length for the permutations I'm looking to generate, but I wouldn't look down on a solution for arbitrary length permutations.

感谢您的帮助!

推荐答案

itertools.permutations(my_list, 3)

这篇关于给定N个项目的列表,如何生成长度LEN的排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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