简体   繁体   中英

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.

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]

Example:

Resulting in lots of 7 character alphanumeric strings

  • CH07YCD
  • CT11MPP
  • etc.

Crap Maths:

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

My question:

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 -> Roughly equivalent to nested for-loops in a generator expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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