Форум сайта python.su
# -*- coding: cp1251 -*- def permute_in_place(a): a.sort() yield list(a) if len(a) <= 1: return first = 0 last = len(a) while 1: i = last - 1 while 1: i = i - 1 if a[i] < a[i+1]: j = last - 1 while not (a[i] < a[j]): j = j - 1 a[i], a[j] = a[j], a[i] # swap the values r = a[i+1:last] r.reverse() a[i+1:last] = r yield list(a) break if i == first: a.reverse() return if __name__ == '__main__': konto=[] file= open('output1.txt', 'rb') for line in file: konto.append(line.strip()) for a in permute_in_place(['с','о','в','л','о']): for ip in konto: if ''.join(a)==''.join(ip): print ''.join(a) #print konto
import itertools konto=[] file= open('output1.txt', 'rb') for line in file: konto.append(line.strip()) slovo=['с','о','о','л','в','д'] d= list(itertools.product(slovo, repeat=5)) n=o for i in d: n += 1 for ip in konto: if ''.join(i)==''.join(ip): print ''.join(i) print 'использовал',n,'комбинаций'
Отредактировано sanodin (Март 17, 2013 12:27:26)
Офлайн
Если правильно понял, на похожий вопрос отвечал на SO
http://stackoverflow.com/a/8929023/1052325
Если длина слов всегда равна длине исходного набора букв, то можно еще проще, комментарий из того же вопроса:
I wrote a program like this once. Easiest thing is to load the entire dictionary into a python dict, where each word is keyed by its sorted letters. That allows you to check for a permuation in O(1): if sorted(word) in dict. But for different lengths, you need to do combinations, using a naive algorithm this is O(2^n) though there may be a better way.
Офлайн
reclosedevспасибо…вопрос закрыт
Если правильно понял, на похожий вопрос отвечал на SOhttp://stackoverflow.com/a/8929023/1052325Если длина слов всегда равна длине исходного набора букв, то можно еще проще, комментарий из того же вопроса:
Офлайн