简体   繁体   English

Python:将字母和数字组合写入文件

[英]Python: writing combinations of letters and numbers to a file

I have been looking at other SO posts in order to create a program that will generate a list of combinations (letters + numbers) given certain parameters, I have gotten as far as this: 我一直在寻找其他SO帖子,以便创建一个程序,根据某些参数生成一个组合列表(字母+数字),我已经得到了这样的结果:

from itertools import product
from string import *

keywords = [''.join(i) for i in product(ascii_letters + digits, repeat =  3)]
file = open("file.txt", "w")

for item in keywords: 
  file.write("%s\n" % item)

file.close()

This program works fine if the repeat parameter is kept to 3/4, but if raised to 5 or above then the program does not finish - it doesn't crash, just never seems to finish. 如果repeat参数保持在3/4,这个程序工作正常,但如果提高到5或更高,那么程序没有完成 - 它不会崩溃,似乎永远不会完成。 I am guessing it is a performance issue, however I am not sure. 我猜这是一个性能问题,但我不确定。 If someone could give a more efficient program it would be most appreciated. 如果有人能提供更有效的计划,那将是非常感激的。

Secondly, I want the program to output both: 其次,我希望程序输出:

  • aec AEC
  • cea CEA

With this current code, it will only output the first. 使用此当前代码,它将仅输出第一个。

product(ascii_letters + digits, repeat=5) generates all 916,132,832 possibilities for the strings ( 62**5 ). product(ascii_letters + digits, repeat=5)为字符串生成所有916,132,832种可能性( 62**5 )。

Your current code is making a list of all of these string objects in memory before writing to the file. 在写入文件之前,您当前的代码会在内存中列出所有这些字符串对象。 This might be too much for your system since each three-letter string object will be around 52 bytes (in Python 3, slightly less in Python 2). 这可能对您的系统来说太多了,因为每个三个字母的字符串对象大约是52个字节(在Python 3中,在Python 2中略少)。 This means you're making about 44GB of Python strings for your list. 这意味着您为列表制作了大约44GB的Python字符串。

Instead, use a generator expression for keywords to avoid holding all the strings in memory (just use (...) rather than [...] ): 相反,使用keywords生成器表达式来避免将所有字符串保存在内存中(只使用(...)而不是[...] ):

keywords = (''.join(i) for i in product(ascii_letters + digits, repeat=5))

You can then iterate and write the strings to a file as before: 然后,您可以像以前一样迭代并将字符串写入文件:

with open("file.txt", "w") as f:
    for item in keywords: 
        f.write(item)
        f.write('\n')

(also, product(ascii_letters + digits, repeat=3) will generate both 'aec' and 'cea'.) (另外, product(ascii_letters + digits, repeat=3) 生成'aec'和'cea'。)

You might want to try this: 你可能想试试这个:

file = open("file.txt", "w")
for item in product(ascii_letters + digits, repeat =  3):
  file.write('%s\n' % ''.join(item))
file.close()

Here we avoid collecting all of the answers into a big array (keywords). 在这里,我们避免将所有答案收集到一个大数组(关键字)。 product() returns a generator, so it is more efficient to iterate over it than to collect all of its responses before iterating. product()返回一个生成器,因此迭代它比在迭代之前收集所有响应更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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