item_49.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:006764 作者: gilbutITbook 项目源码 文件源码
def find_anagrams(word, dictionary):
    """Find all anagrams for a word.

    This function only runs as fast as the test for
    membership in the 'dictionary' container. It will
    be slow if the dictionary is a list and fast if
    it's a set.

    Args:
        word: String of the target word.
        dictionary: Container with all strings that
            are known to be actual words.

    Returns:
        List of anagrams that were found. Empty if
        none were found.
    """
    permutations = itertools.permutations(word, len(word))
    possible = (''.join(x) for x in permutations)
    found = {word for word in possible if word in dictionary}
    return list(found)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号