查找给定字符串中包含的字谜

Finding anagrams contained in a given string

我正在尝试编写一个接受字符串并将变位词分组的程序 list of lists 中的字符串,按字典顺序排序。

例如下面的字符串:

eat tea tan ate nat bat

应该产生以下输出(行的顺序很重要):

ate eat tea
bat
nat tan

我写的程序:

from collections import defaultdict

def get_anagrams(source):
d = defaultdict(list)
for word in source:
    key = "".join(sorted(word))
    d[key].append(word)
return d

def print_anagrams(my_string):
word_source = my_string.split(" ")
d = get_anagrams(word_source)
for key, anagrams in d.items():
    print(" ".join(sorted(anagrams)))

print_anagrams("eat tea tan ate nat bat")

该程序生成正确的字谜,但每次我 运行 程序的行顺序与预期输出相比都会发生变化。

所以有时我得到

nat tan
ate eat tea
bat

其他时候我确实得到了正确的输出

ate eat tea
bat
nat tan

有人可以指出我做错了什么吗?

你有一个 dictionary ,如果你用 for key, anagrams in d.items(): 迭代它,你无法保证顺序:

The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it). To check whether a single key is in the dictionary, use the in keyword.

因此,您将像这样编辑代码,以迭代排序的字典按键排序):

for key, anagrams in sorted(d.items()):
    print(" ".join(sorted(anagrams)))

这保证输出总是

bat
ate eat tea
nat tan

字典键的顺序是随机设计的。

如果您想按照在原始文本中出现的顺序打印字谜,请使用 OrderedDict 以与您插入的顺序相同的顺序存储键:

from collections import OrderedDict

def get_anagrams(source):
    d = OrderedDict()
    for word in source:
        key = "".join(sorted(word))
        if key not in d:
            d[key] = []
        d[key].append(word)
    return d

def print_anagrams(my_string):
    word_source = my_string.split(" ")
    d = get_anagrams(word_source)
    for key, anagrams in d.items():
        print(" ".join(sorted(anagrams)))

print_anagrams("eat tea tan ate nat bat")

输出:

ate eat tea
nat tan
bat