2016-09-08 3 views
-1

제 질문은 간단합니다. 그리고 그 중 절반은 이미 작동 중입니다. 정렬 된 단어 순열 생성에 도움이 필요합니다.파이썬에서 단어 perminuations을 주문했습니다.

내 코드 :

from os.path import isfile 
from string import printable 

def loadRuleSet(fileLocation): 
    rules = {} 
    assert isfile(fileLocation) 
    for x in open(fileLocation).read().split('\n'): 
     if not len(x) == 0: 
      data = x.split(':') 
      if not len(data[0]) == 0 or not len(data[1]) == 0: 
       rules[data[0]] = data[1] 
    return rules 

class deform: 
    def __init__(self, ruleSet): 
     assert type(ruleSet) == dict 
     self.ruleSet = ruleSet 

    def walker(self, string): 
     spot = [] 
     cnt = 0 
     for x in string: 
      spot.append((x, cnt)) 
      cnt += 1 
     return spot 


    def replace_exact(self, word, position, new): 
     cnt = 0 
     newword = '' 
     for x in word: 
      if cnt == position: 
       newword += new 
      else: 
       newword += x 
      cnt+= 1 
     return newword 


    def first_iter(self, word): 
     data = [] 
     pos = self.walker(word) 
     for x in pos: 
      if x[0] in self.ruleSet: 
       for y in self.ruleSet[x[0]]: 
        data.append(self.replace_exact(word, x[1], y)) 
     return data 

print deform({'a':'@A'}).first_iter('abac') 

내 현재 코드는 작업의 절반을 수행하지만, 나는 "작가의 블록"

>>>deform({'a':'@'}).first_iter('aaa') 

['@aa', '[email protected]', '[email protected]'] 

가 여기 내 현재 만든 스크립트의 결과를의에 도달했습니다.

코드에서 수행해야 할 작업은 다음과 같습니다. - 단어를 가져 와서 대체 단어의 다른 문자로 재정렬하십시오. 나는 성공적으로 그것을 한 캐릭터와 함께 만들었지 만 모든 결과를 만드는 데 도움이 필요합니다. 예를 들어

귀하의 경우 가능한 모든 순서 부, 아니 반복 요소를 반환 할 수 permutations 기능을 사용할 수 있습니다에서
['@aa', '[email protected]', '[email protected]', '@@a', '[email protected]@', '@[email protected]'] 
+3

질문에 텍스트로 코드를 포함하십시오. 예를 들어 내 방화벽으로 인해 내 링크가 표시되지 않습니다. –

답변

2

. 그들은 @a 문자 수를 다르기 때문에

from itertools import permutations 
from operator import itemgetter 

perm_one = sorted(set([''.join(x) for x in permutations('@aa')])) 
perm_two = sorted(set([''.join(x) for x in permutations('@@a')]), key=itemgetter(1)) 
print perm_one + perm_two 

나는 두 개의 컬렉션으로 나누어.

+0

어떻게 작동하는지 설명하는 문장이 거의 없으면 좋을 것입니다. – kosa

관련 문제