数字和字母的组合
#!/usr/bin/python
import random
lower_a = ['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']
upper_a = ['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']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
all = []
all = " ".join("".join(lower_a) + "".join(upper_a) + "".join(num))
all = all.split()
x = 1
c = 1
while x < 10:
y = []
for i in range(c):
a = random.choice(all)
y.append(a)
print "".join(y)
x += 1
c += 1
我现在输出的内容如下:
5
hE
HAy
1kgy
Pt6JM
2pFuCb
Jv5osaX
5q8PwWAO
SvHWRKfI5
如何让它系统地遍历给定长度的字母(大写和小写)的每个组合,然后在该长度上加 1 并重复该过程?
#!/usr/bin/python
import random
lower_a = ['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']
upper_a = ['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']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
all = []
all = " ".join("".join(lower_a) + "".join(upper_a) + "".join(num))
all = all.split()
x = 1
c = 1
while x < 10:
y = []
for i in range(c):
a = random.choice(all)
y.append(a)
print "".join(y)
x += 1
c += 1
What I have now outputs something like the following:
5
hE
HAy
1kgy
Pt6JM
2pFuCb
Jv5osaX
5q8PwWAO
SvHWRKfI5
How can I make it systematically go through every combination of letters (upper and lowercase) for a given length, then add 1 to that length and repeat the process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好不要重新创建标准库中已有的功能。
看一下标准库模块
itertools
。特别是combinations()
,permutations()
,和product()
函数。如果您的 Python 版本较旧,您可能无法访问这些函数。但是,如果您查看 Python 2.6 的文档,您可以了解如何在 Python 中实现所有这些功能。例如,itertools.product 的实现如下:
您也可以尝试递归解决方案:
It's best not to recreate functionality that is already in the standard library.
Take a look at the standard library module
itertools
. Particularly thecombinations()
,permutations()
, andproduct()
functions.If your version of Python is old you may not have access to these functions. However if you take a look in the documentation for Python 2.6, you can see how all of these functions can be implemented in Python. For instance, the implementation of
itertools.product
is given as:You could also try a recursive solution instead:
pythonic 方式 ;)
打印所有组合:
更好的是,创建一个生成组合的生成器对象,以便稍后可以决定如何处理它们,而不必存储
2 ** 62
字符串 (< code>7.6040173890593902e+35 字节)在内存中。combinations
和product
以及许多其他函数都返回迭代器而不是列表,以节省内存:The pythonic way ;)
Print all combinations:
Even better, create an generator object which yields the combinations, so that one can decide later what to do with them without having to store
2 ** 62
strings (7.6040173890593902e+35
bytes) in memory.Both
combinations
andproduct
, as well as many other functions, return iterators instead of lists in order to save memory:看一下 itertools 模块中的函数组合 (http://docs.python.org/library/itertools.html#itertools.combinations)
Take a look at function combinations in the module itertools (http://docs.python.org/library/itertools.html#itertools.combinations)
我还没有对此进行测试,但我认为基本思想应该成立。如果不起作用请评论,我会调试它:
I haven't tested this, but I think the basic idea should hold. Please comment if it doesn't work and I'll debug it: