python - 如何在 Python 中通过重复获得每个 6 元素排列?
问题描述
我想从“abcdefghijklmnopqrstuvwxyz0123456789”创建所有可能的 6 元素排列的列表,例如它应该输出:
['aaaaaa','aaaaab','aaaaac'...,'aaaaa0','aaaaa1'...,'aaaaba','aaaabb'...]
等等。
这是我尝试过的:
import itertools
dictionary = 'abcdefghijklmnopqrstuvwxyz0123456789'
print(list(itertools.product(dictionary, repeat=6)))
但是我遇到了一个MemoryError
然后我的电脑完全死机了,那么有没有更有效的方法来计算这个列表?
(我使用的是 Python 3.8 64 位)
解决方案
你知道你的名单会有多长吗?它是 36**6 = 2176782336 项。有点太多记不住了。您应该使用生成器:
dictionary = 'abcdefghijklmnopqrstuvwxyz0123456789'
for x in itertools.product(dictionary, repeat=6):
print(''.join(x))
推荐阅读
- sql - 运行 sp_depends 系统过程时,如果列“column”为 NULL 是什么意思
- java - java.lang.SecurityException onRequestPermissionsResult
- javascript - 如何避免一个音符覆盖另一个音符?(本地存储)
- python - 从彭博社提取财务数据
- qt - QML TextField 设置焦点突出显示颜色
- linux - 返回提示模式前修改二进制输出
- haskell - 在 ReaderT 上获取带有透镜的元组子集
- python - AttributeError:“str”对象没有属性“get”或最多返回 None
- python - Mac上的Python - 找不到文件
- azure - 引起:MqttException (0) - java.net.UnknownHostException: xxx..azure-devices.net