2015-09-10 20 views
5

Come posso fare in modo che questo script permuti il ​​massimo di 3 combinazioni di parole da un elenco?Python wordlist permutation

List.txt consiste di 4 corde:

pass 
10 
test 
word 

Invece di combinare tutti da uno a quattro parole esempio

output > pass10wordtest, 10testpassword,....etc 

voglio la combinazione finale sia:

output > testpassword, passtestword, 10wordtest,....etc 

Il mio codice:

from itertools import permutations 
import os 

# GET FILE 
script_dir = os.path.dirname(os.path.realpath(__file__)) 
wordlist_rel_path = "List.txt" 
wordlist_abs_file_path = os.path.join(script_dir, wordlist_rel_path) 

# READ WORD LIST FROM FILE 
word_list = [] 
print ("\ninput file is:", wordlist_abs_file_path,"\n") 
with open(wordlist_abs_file_path) as wordlist: 
    for line in wordlist: 
     word_list.append(line.rstrip()) 

# PRINT INPUT LIST 
print ("input list contains:") 
print(word_list,"\n") 

# GENERATE POWERSET 
powerset_list = [] 
print ("output list is:") 
for n in range(1, len(word_list)+1): 
    for perm in permutations(word_list, n): 
     powerset_list.append("".join(perm)) 
print(powerset_list) 

# WRITE LIST TO FILE 
powerset_rel_path = "powerset.txt" 
powerset_abs_file_path = os.path.join(script_dir, powerset_rel_path) 
powerset_abs_file = open(powerset_abs_file_path, 'w') 
for item in powerset_list: 
    powerset_abs_file.write("%s\n" % item) 
powerset_abs_file.close() 

risposta

6

Come è possibile consentire il massimo di 3 combinazioni di parole da un elenco?

La funzione permutations supporta un secondo argomento per selezionare solo tre ingressi alla volta:

>>> from itertools import permutations 
>>> for group in permutations(['pass', '10', 'test', 'word'], 3): 
     print(''.join(group)) 

pass10test 
pass10word 
passtest10 
passtestword 
password10 
passwordtest 
10passtest 
10password 
10testpass 
10testword 
10wordpass 
10wordtest 
testpass10 
testpassword 
test10pass 
test10word 
testwordpass 
testword10 
wordpass10 
wordpasstest 
word10pass 
word10test 
wordtestpass 
wordtest10 
+1

funziona benissimo! Grazie! – sfad

1
from itertools import permutations 
import os 

# GET FILE 
script_dir = os.path.dirname(os.path.realpath(__file__)) 
wordlist_rel_path = "List.txt" 
wordlist_abs_file_path = os.path.join(script_dir, wordlist_rel_path) 

# READ WORD LIST FROM FILE 
word_list = [] print ("\ninput file is:", wordlist_abs_file_path,"\n") 
with open(wordlist_abs_file_path) as wordlist: 
    for line in wordlist: 
     word_list.append(line.rstrip()) 

# PRINT INPUT LIST 
print ("input list contains:") 
print(word_list,"\n") 

# GENERATE POWERSET 
powerset_list = [] 
print ("output list is:") 
for n in range(1, len(word_list)+1): 
    for perm in permutations(word_list,3): 
     powerset_list.append("".join(perm)) print(powerset_list) 

# WRITE LIST TO FILE 
powerset_rel_path = "powerset.txt" 
powerset_abs_file_path = os.path.join(script_dir, powerset_rel_path) 
powerset_abs_file = open(powerset_abs_file_path, 'w') for item in powerset_list: 
powerset_abs_file.write("%s\n" % item) powerset_abs_file.close()