我想在我创建的字典中找到最常用的单词。 我读到它,我看到答案是使用排序函数和[:n]来询问元素数量。
我的问题是我的字典与我看到的有点不同 - 它就是这样:
[{"count":27,"stem":"obama","term":"obama"},{"count":20,"stem":"boehner","term":"boehner"},{"count":4,"stem":"tax","term":"tax"},
{"count":3,"stem":"daley","term":"daley"},{"count":3,"stem":"couldn","term":"couldn"},{"count":2,"stem":"trillion","term":"trillion"}]
所以在这个例子中奥巴马提到27次博纳20和税2 - 所以我想说我想得到前5个最常见的单词,我该怎么做?
答案 0 :(得分:2)
In [39]: L = [{"count":27,"stem":"obama","term":"obama"},{"count":20,"stem":"boehner","term":"boehner"},{"count":4,"stem":"tax","term":"tax"},
{"count":3,"stem":"daley","term":"daley"},{"count":3,"stem":"couldn","term":"couldn"},{"count":2,"stem":"trillion","term":"trillion"}]
In [40]: counts = collections.Counter(itertools.chain.from_iterable([d['term']]*d['count'] for d in L))
In [41]: counts.most_common(5)
Out[41]: [('obama', 27), ('boehner', 20), ('tax', 4), ('daley', 3), ('couldn', 3)]
别忘了import itertools, collections