本文整理汇总了Python中bintrees.FastRBTree.iter_items方法的典型用法代码示例。如果您正苦于以下问题:Python FastRBTree.iter_items方法的具体用法?Python FastRBTree.iter_items怎么用?Python FastRBTree.iter_items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bintrees.FastRBTree
的用法示例。
在下文中一共展示了FastRBTree.iter_items方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_top_k_with_FastRBTree
# 需要导入模块: from bintrees import FastRBTree [as 别名]
# 或者: from bintrees.FastRBTree import iter_items [as 别名]
def find_top_k_with_FastRBTree(filename = TDATA, k = 10):
"""
Profile result:
5 million strings:
memory consuming: 259 MB
time consuming: 39.9689998627
[(753, 'bf'),
(753, 'qj'),
(753, 'zb'),
(753, 'vz'),
(763, 'ma'),
(755, 'lx'),
(779, 'qp'),
(768, 'bg'),
(758, 'eq'),
(767, 'tf')]
"""
result = []
t = FastRBTree()
with open(filename) as f:
for line in f:
key = line.strip()
t[key] = t.setdefault(key, 0) + 1
# heapq
for key, val in t.iter_items():
if len(result) < k:
heapq.heappush(result, (val, key))
else:
heapq.heappushpop(result, (val, key))
return result