簡體   English   中英

試圖在 Python 中寫出所有可能的 4 個字符組合的列表

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

基本上我希望所有可能的 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")

與替換組合 - 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種可能性。 (下次使用術語排列:)切掉此列表中的前 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. 不要使用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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM