输出多个列表的所有可能排列 [英] Outputting all possible permutations of multiple lists

查看:151
本文介绍了输出多个列表的所有可能排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,刚买了我的第一本"Python速成班"书-最初,我选择的语言是PHP.

I am very new to Python and have only just bought my first "Crashcourse in Python" book - originally my choice of language was PHP.

我的目标:

我想要一个脚本,该脚本将在屏幕上输出特定模式的所有可能排列的列表.顺序并不重要.

I desire a script that will output on-screen a list of all possible permutations of a particular pattern. Order is unimportant.

原始数据和模式(数据集将保持不变):

The raw data and pattern (the dataset will not change):

List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY']
List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60', '11', '61', '12', '62', '13', '63', '14', '64', '15', '65', '16', '66', '17']
List3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List4 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

字符串输出:

[List1] + [List2] + [List3] + [List4] + [List5]

[List1] + [List2] + [List3] + [List4] + [List5]

示例:

产生大量7个字符的字母数字字符串

  • CH07YCD
  • CT11MPP

废话数学:

我的数学运算是否正确,因为我要查看10,174,464个条目? List1(23)x List2(32)x List3,4,5(13,824).

Is my wonky maths correct in that I'd be looking at 10,174,464 entries? List1(23) x List2(32) x List3,4,5(13,824).

我的问题:

itertools是否是用于此目的的最佳功能?如果是这样,怎么办?如果没有,怎么办?

Is itertools the best function to use for this? If so, how? If not, what?

推荐答案

>>> import itertools
>>> s = [List1, List2, List3, List4, List5]
>>> n = list(itertools.product(*s))
>>> len(n)
10174464

itertools.product->大致等同于生成器表达式中的嵌套for循环.

itertools.product -> Roughly equivalent to nested for-loops in a generator expression.

这篇关于输出多个列表的所有可能排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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