【发布时间】:2017-12-04 18:03:53
【问题描述】:
我得到一个字母序列,并且必须生成给定序列的所有 N 长度字谜,其中 N 是序列的长度。
我在 python 中遵循一种有点幼稚的方法,我正在采用所有排列来实现这一点。我发现了一些类似的线程,例如this one,但我更喜欢 Python 中的面向数学的方法。那么有什么比排列更高效的替代方案呢?我下面的尝试有什么特别错误的吗?
from itertools import permutations
def find_all_anagrams(word):
pp = permutations(word)
perm_set = set()
for i in pp:
perm_set.add(i)
ll = [list(i) for i in perm_set]
ll.sort()
print(ll)
【问题讨论】:
标签: python algorithm permutation anagram