Fi中置换搜索的优化

2024-10-01 15:44:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个程序,使用一组字符,在文件中搜索该组字符的任何排列。我很想听听你对优化这个计划的建议

根据每个“单词”的字符数,我将文件拆分为多个:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import itertools
import time

start_time = time.time()
chrset = sys.argv[1]
lgr = len(chrset)
f = open('gd'+str(lgr), 'r')
perms = []
s = list(itertools.permutations(chrset))

for perm in s:
    perms.append(''.join(map(str,perm)))

for line in f:
    line = line.rstrip('\n')
    for pp in perms:
        if pp == line: 
            print (line)    

print("--- %s seconds ---" % (time.time() - start_time))

Tags: 文件inimportfortimesysline字符

热门问题