我们知道python内建模块的collections有很多好用的操作。
比如:
from collections import Counter
#统计字符串
# top n问题
user_counter = Counter("abbafafpskaag")
print(user_counter.most_common(3)) #[('a', 5), ('f', 2), ('b', 2)]
print(user_counter['a']) # 5
如果用js你会怎样实现?或者有造好的轮子里面有这样的操作?
python里面实现most_common:
def most_common(self, n=None):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
'''
# Emulate Bag.sortedByCount from Smalltalk
if n is None:
return sorted(self.items(), key=_itemgetter(1), reverse=True)
return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
这里用到了个 _heapq
堆数据结构 也就是说它是堆来解决top n问题的,而不是遍历。 js有哪些类似的轮子
`另注:`遍历的方法可以通过str.split()
实现
其实这个问题的实质是 使用堆实现Top K算法(JS实现)
我没有用过,应该有,毕竟 npm 里那么多库,不过没检索过就是了。
我觉得对于 JS 来说,最大的问题是你不好确定是
这样比较快还是手写一个堆排序比较快。
另外就这个问题而言,我觉得直接遍历一下字符串也行,复杂度是 O(n)。因为 JS 里也是没有
counter
方法的,哈哈。