尝试在Python中写一个所有可能的4个字符组合的列表。

问题描述 投票:0回答:1

基本上我想所有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")
python itertools
1个回答
1
投票

随着组合与替换 - 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)

有组合无替换--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)

有无替换的排列组合 - 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)

有替换的排列组合--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))

最有可能的是你想要的是带替换的组合,因为你指定了 38^4 总的可能性。(下次用permutations这个词!)把这个列表中的前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']

1
投票

运行这个差点把我的笔记本烧坏了

对代码的一些改进。

  1. 不要使用 tester.write(comb),因为你不能写一个 tuple 到一个文件。只有字符串(str). 所以我决定使用 tester.write(str(comb)).

  2. 你可能应该找到一个更好的方法来做任何你想做的事情,因为创建这种大小的文件,几乎杀死一台笔记本电脑的方法可能不是最有效的方法^^。

改进的代码。

#! /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))
© www.soinside.com 2019 - 2024. All rights reserved.