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


Python Bag.subtract方法代码示例

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


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

示例1: Bag

# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import subtract [as 别名]
    dict_hash_table = dict.snarf_dictionary(options.dict_fn)

    the_phrase = Bag(args[0])
    print("Pruning dictionary.  Before: {} bags ...".format(len(dict_hash_table.keys())),
          file=sys.stderr, end='')

    # Now convert the hash table to a list, longest entries first.  (This
    # isn't necessary, but it makes the more interesting anagrams appear
    # first.)

    # While we're at it, prune the list, too.  That _is_ necessary for the
    # program to finish before you grow old and die.

    the_dict_list = [[k, dict_hash_table[k]]
                     for k in dict_hash_table.keys()
                     if the_phrase.subtract(k)]

    # Note that sorting entries "alphabetically" only makes partial sense,
    # since each entry is(at least potentially) more than one word(all
    # the words in an entry are anagrams of each other).
    def biggest_first_then_alphabetically(a, b):
        a = a[1][0]
        b = b[1][0]
        result = cmp(len(b), len(a))
        if not result:
            result = cmp(a, b)
        return result

    the_dict_list.sort(biggest_first_then_alphabetically)

    print(" After: {} bags.".format(len(the_dict_list)),
开发者ID:tdhale,项目名称:anagrams,代码行数:33,代码来源:anagrams.py

示例2: Bag

# 需要导入模块: from bag import Bag [as 别名]
# 或者: from bag.Bag import subtract [as 别名]
    args = parser.parse_args()

    dict_hash_table = dict.snarf_dictionary_from_file(args.dictionary)

    the_phrase = Bag("".join(args.words))
    print("Pruning dictionary.  Before: {} bags ...".format(len(dict_hash_table.keys())), file=sys.stderr, end="")

    # Now convert the hash table to a list, longest entries first.  (This
    # isn't necessary, but it makes the more interesting anagrams appear
    # first.)

    # While we're at it, prune the list, too.  That _is_ necessary for the
    # program to finish before you grow old and die.

    the_dict_list = sorted([[k, dict_hash_table[k]] for k in dict_hash_table.keys() if the_phrase.subtract(k)], key=len)

    print(" After: {} bags.".format(len(the_dict_list)), file=sys.stderr)

    pr = cProfile.Profile()
    pr.enable()
    result = anagrams(the_phrase, the_dict_list)
    pr.disable()

    ps = pstats.Stats(pr, stream=sys.stderr).sort_stats("cumulative")
    ps.print_stats()

    print("{} anagrams of {}".format(len(result), " ".join(args.words)), file=sys.stderr)
    for a in result:
        sys.stdout.write("(")
        for i, w in enumerate(a):
开发者ID:offby1,项目名称:anagrams,代码行数:32,代码来源:anagrams.py


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