简体   繁体   中英

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:

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. 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
  • 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 ).

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). This means you're making about 44GB of Python strings for your list.

Instead, use a generator expression for keywords to avoid holding all the strings in memory (just use (...) rather than [...] ):

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'.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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