简体   繁体   English

试图在 Python 中写出所有可能的 4 个字符组合的列表

[英]trying to write a list of all possible 4 characters combinations in Python

basically i want all 4 characters combinations possible written in a txt file The problem is duplication should be allowed, i want the combinations 1111,2222... Where do you think i went wrong and how would you fix it?基本上我希望所有可能的 4 个字符组合都写在一个 txt 文件中问题是应该允许重复,我想要组合 1111,2222...你认为我哪里出错了,你将如何解决它?

import itertools
import sys
import os

tester = open(r"available.txt","a")
lol =[]
a = [1,2,3,4,5,6,7,8,9,0,'_','.','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']

lol=list(itertools.combinations(a, 4))
for comb in lol:
    tester.write(str(comb)+"\n")

With combinations with replacement - 101270 entries: (runs instantly excluding file IO)与替换组合 - 101270 个条目:(立即运行,不包括文件 IO)

import itertools
with open('available.txt', 'w') as f:
    lol = []
    a = [i for i in '1234567890._abcdefghijklmnopqrstuvwxyz']
    lol = list(itertools.combinations_with_replacement(a, 4))
    for comb in lol:
        f.write(comb)

With combinations without replacement - 73815 entries: (runs instantly excluding file IO)无需替换的组合 - 73815 个条目:(立即运行,不包括文件 IO)

import itertools
with open('available.txt', 'w') as f:
    lol = []
    a = [i for i in '1234567890._abcdefghijklmnopqrstuvwxyz']
    lol = list(itertools.combinations(a, 4))
    for comb in lol:
        f.write(comb)

With permutations without replacement - 1771560 entries: (runs instantly excluding file IO)无需替换的排列 - 1771560 个条目:(立即运行,不包括文件 IO)

import itertools
with open('available.txt', 'w') as f:
    lol = []
    a = [i for i in '1234567890._abcdefghijklmnopqrstuvwxyz']
    lol = list(itertools.permutations(a, 4))
    for comb in lol:
        f.write(comb)

With permutations with replacement - 2085136 entries: (runs in about 2 seconds excluding file IO)带有替换的排列 - 2085136 个条目:(在大约 2 秒内运行,不包括文件 IO)

lol = []
for a in '1234567890._abcdefghijklmnopqrstuvwxyz':
    for b in '1234567890._abcdefghijklmnopqrstuvwxyz':
        for c in '1234567890._abcdefghijklmnopqrstuvwxyz':
            for d in '1234567890._abcdefghijklmnopqrstuvwxyz':
                lol.append(a+b+c+d)
with open('my_dump.txt', 'w') as f:
    f.write(repr(lol))

Most likely you wanted permutations with replacement, as you specified 38^4 total possibilities.很可能您需要替换排列,因为您指定了38^4种可能性。 (Use the term permutations next time:) Slicing off the first 100 entries in this list: (下次使用术语排列:)切掉此列表中的前 100 个条目:

>>> lol[:100]
['1111', '1112', '1113', '1114', '1115', '1116', '1117', '1118', '1119', '1110', '111.', '111_', '111a', '111b', '111c', '111d', '111e', '111f', '111g', '111h', '111i', '111j', '111k', '111l', '111m', '111n', '111o', '111p', '111q', '111r', '111s', '111t', '111u', '111v', '111w', '111x', '111y', '111z', '1121', '1122', '1123', '1124', '1125', '1126', '1127', '1128', '1129', '1120', '112.', '112_', '112a', '112b', '112c', '112d', '112e', '112f', '112g', '112h', '112i', '112j', '112k', '112l', '112m', '112n', '112o', '112p', '112q', '112r', '112s', '112t', '112u', '112v', '112w', '112x', '112y', '112z', '1131', '1132', '1133', '1134', '1135', '1136', '1137', '1138', '1139', '1130', '113.', '113_', '113a', '113b', '113c', '113d', '113e', '113f', '113g', '113h', '113i', '113j', '113k', '113l']

Running this almost fried my laptop.运行这个几乎炸了我的笔记本电脑。

A few improvements for the code:代码的一些改进:

  1. Do not use tester.write(comb) , as you can't write a tuple to a file.不要使用tester.write(comb) ,因为您不能将tuple写入文件。 Only strings ( str ).只有字符串( str )。 So I decided use tester.write(str(comb)) .所以我决定使用tester.write(str(comb))

  2. You should probably find a better way of doing whatever you want to do, because a way that creates files of this size and almost kills a laptop is probably not the most efficient way ^^您可能应该找到一种更好的方法来做任何您想做的事情,因为创建这种大小的文件并几乎杀死笔记本电脑的方法可能不是最有效的方法^^

Improved Code:改进的代码:

#! /usr/bin/python3

import itertools
import sys
import os

tester = open(r"available.txt","a")
lol =[]
a = ['1','2','3','4','5','6','7','8','9','0','_','.','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']

lol=list(itertools.combinations(a, 4))

for comb in lol:
    tester.write(str(comb))

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

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