顺序生成字母数字字符串 [英] Generate alphanumeric strings sequentially

查看:132
本文介绍了顺序生成字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个循环来生成和打印字符串,如下所示:

I'm trying to create a loop to generate and print strings as follows:


  1. 仅字母数字字符:

  2. 0-9在AZ之前,在z之前,

  3. 长度最多增加4个字符。

因此,它将打印:


  1. 所有0-z字符串

  2. 然后从00-zz

  3. 然后从000-zzz

  4. 然后从0000-zzzz

  1. all strings from 0-z
  2. then from 00-zz
  3. then from 000-zzz
  4. then from 0000-zzzz

然后停止。

推荐答案

from string import digits, ascii_uppercase, ascii_lowercase
from itertools import product

chars = digits + ascii_uppercase + ascii_lowercase

for n in range(1, 4 + 1):
    for comb in product(chars, repeat=n):
        print ''.join(comb)

首先创建一个由所有数字,大写字母和小写字母组成的字符串。

This first makes a string of all the numbers, uppercase letters, and lowercase letters.

然后,从1-开始,每个长度4,打印所有可能的c

Then, for each length from 1-4, it prints every possible combination of those numbers and letters.

请记住,这是很多组合-62 ^ 4 + 62 ^ 3 + 62 ^ 2 + 62。

Keep in mind this is A LOT of combinations -- 62^4 + 62^3 + 62^2 + 62.

这篇关于顺序生成字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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