简体   繁体   English

输出多个列表的所有可能排列

[英]Outputting all possible permutations of multiple lists

I am very new to Python and have only just bought my first "Crashcourse in Python" book - originally my choice of language was PHP. 我是Python的新手,刚买了我的第一本“ Python速成班”书-最初,我选择的语言是PHP。

My Objective: 我的目标:

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']

String Output: 字符串输出:

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

Example: 例:

Resulting in lots of 7 character alphanumeric strings 产生大量7个字符的字母数字字符串

  • CH07YCD CH07YCD
  • CT11MPP CT11MPP
  • etc. 等等

Crap Maths: 废话数学:

Is my wonky maths correct in that I'd be looking at 10,174,464 entries? 我会根据10,174,464个条目进行数学运算,是否正确? List1(23) x List2(32) x List3,4,5(13,824). List1(23)x List2(32)x List3,4,5(13,824)。

My question: 我的问题:

Is itertools the best function to use for this? itertools是为此使用的最佳功能吗? If so, how? 如果是这样,怎么办? If not, what? 如果没有,那该怎么办?

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

itertools.product -> Roughly equivalent to nested for-loops in a generator expression. itertools.product->大致等效于生成器表达式中的嵌套for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM