当前位置: 首页>>代码示例>>Python>>正文


Python Trie.create方法代码示例

本文整理汇总了Python中trie.Trie.create方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.create方法的具体用法?Python Trie.create怎么用?Python Trie.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.create方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: search_in_board

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import create [as 别名]
def search_in_board(words, board):
    trie = Trie.create(words+words[::-1])
    acc_hash = {}
    handled_paths = []
    pos_list = [(i,j) for i in range(len(board)) for j in range(len(board[0]))]
    while len(pos_list) > 0:
        i,j = pos_list.pop(0)
        cur_char = board[i][j]
        # ((0,0),'o',[])
        cur_word_point = ([(i,j)], cur_char)
        # [((1,0),'e'),((0,1),'a')]
        neighbors = find_neighbors((i,j),board)
        cur_words = acc_hash.get((i,j), [])
        # remove all the paths which have been handled
        cur_words = filter(lambda x: x[0] not in handled_paths, cur_words)
        filtered_prefixs = filter_by_prefix(
                cur_words+[cur_word_point], neighbors, trie)
        # [((0,1),'oa',[(0,0)])]
        update_acc_hash(acc_hash, filtered_prefixs)
        # add all the paths which have been handled
        map(lambda x: handled_paths.append(x[0]), cur_words)
        # add some position for new path
        for cur_word_point in filtered_prefixs:
            cur_pos = cur_word_point[0][-1]
            if cur_pos not in pos_list:
                pos_list.append(cur_pos)


    # return acc_hash
    word_points = filter_words(acc_hash)
    return map(lambda x: (x[1], x[0]), word_points)
开发者ID:RobLeggett,项目名称:codes,代码行数:33,代码来源:word_search_2.py

示例2: test

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import create [as 别名]
              ['i','f','l','v']
            ]

    words = ["oath","pea","eat","rain"]

    with test(check_in_board_range):
        check_in_board_range((0,0), (3,2)).must_true()
        check_in_board_range((3,0), (3,2)).must_false()
        pass  

    with test(find_neighbors):
        find_neighbors((0,0), board).must_equal(
                [((1, 0), 'e'), ((0, 1), 'a')])

    with test(filter_by_prefix):
        trie = Trie.create(words)
        cur_word_point = ([(0,0)], "o")
        neighbors = [((1, 0), 'e'), ((0, 1), 'a')]
        filter_by_prefix([cur_word_point], 
                neighbors,trie).must_equal(
                [([(0, 0), (0, 1)], 'oa', False)])

    with test(update_acc_hash):
        acc_hash = {}
        filtered_prefixs = [([(0, 0), (0, 1)], 'oa', False)]
        update_acc_hash(acc_hash, filtered_prefixs).must_equal(
                {(0, 1): [([(0, 0), (0, 1)], 'oa', False)]})
        acc_hash.must_equal(
                {(0, 1): [([(0, 0), (0, 1)], 'oa', False)]})
    with test(search_in_board):
        search_in_board(words, board).pp()
开发者ID:RobLeggett,项目名称:codes,代码行数:33,代码来源:word_search_2.py


注:本文中的trie.Trie.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。