如何按照一种格式生成可能的组合列表?

2024-09-29 23:21:08 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我想学习如何使用以下格式创建所有可能组合的列表:

AA0AA0A

它需要是这样的,A-Z 0-9,但是保持字符的位置,如上所示


Tags: 列表格式字符aa0aa0a
3条回答

这是可能的,有976562500种可能性

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ["0","1","2","3","4","5","6","7","8","9"]

combination = []
x = 0
for a in letters:
  for b in letters:
    for c in numbers:
      for d in letters:
        for e in letters:
          for f in numbers:
            for g in letters:
              combination.append(a+b+c+d+e+f+g)

小心!这个计算可能需要一段时间。如果RAM不超过16BG,也不要尝试运行此脚本

如果您使用的是JavaScript,则会有Randexp生成与给定正则表达式匹配的随机字符串

Releases for browser

使用生成器

注意:更新答案,并提供@ShadowRange、@Kelly Bundy、@supery rain)的建议

from string import ascii_uppercase, digits
from itertools import product

def all_combinations():
    ' pattern AA0AA0A '
    A, O = ascii_uppercase, digits
    # Use itertools product to generate all combinations of of 
    # lists of letters and digits
    combs = product(A, A, O, A, A, O, A)
    return map(''.join, combs)
   
# Test
seq = all_combinations() # create generation for combinations

# Show first 20
for i in range(20):
    print(next(seq))

输出

AA0AA0AA
AA0AA0AB
AA0AA0AC
AA0AA0AD
AA0AA0AE
AA0AA0AF
AA0AA0AG
AA0AA0AH
AA0AA0AI
AA0AA0AJ
AA0AA0AK
AA0AA0AL
AA0AA0AM
AA0AA0AN
AA0AA0AO
AA0AA0AP
AA0AA0AQ
AA0AA0AR
AA0AA0AS
AA0AA0AT

相关问题 更多 >

    热门问题