从词典列表中获取前5个值?

时间:2016-12-22 08:59:16

标签: python json list dictionary

我从api键获取json。 这是我的json:

{'[{"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":"formul","term":"formulation"}
  ]' : ' '}

我希望从这个json获得前5个值。我把它变成了词典列表:

def changetodict(data):
    json_str = ast.literal_eval(json.dumps(data))
    #common = json.loads(json_str)
    commonDict = dict(itertools.izip_longest(*[iter(json_str)] * 2,
                                             fillvalue=""))
    print commonDict

这是所有代码:

import urllib2, mediacloud, json,itertools,ast
from collections import Counter

webUrl = urllib2.urlopen("https://api.mediacloud.org/api/v2/wc/list?q=obama+AND+media_id:1&stats=1&&fq=publish_date:%5B2012-04-01T00:00:00.000Z+TO+2012-04-01T00:00:00.000Z%5D&key= ")

def changetodict(data):
    json_str = ast.literal_eval(json.dumps(data))
    #common = json.loads(json_str)
    commonDict = dict(itertools.izip_longest(*[iter(json_str)] * 2, fillvalue=""))
    print commonDict 

most_common_imdb_value = Counter(b['key'] for b in commonDict).most_common(1)[0]

if(webUrl.getcode()== 200):
    data = webUrl.readlines()
    changetodict(data)
else:
    print "error"

我需要更改哪些内容才能生效?

2 个答案:

答案 0 :(得分:1)

>>> items = [{"count":27,"stem":"obama","term":"obama"},{"count":20,"stem":"boehner","term":"boehner"},{"count":4,"stem":"tax","term":"tax"},{"count":3,"stem":"couldn","term":"couldn"},{"count":3,"stem":"daley","term":"daley"}]
>>> from heapq import nlargest
>>> nlargest(3, items, key=lambda item: item["count"])
[{'count': 27, 'term': 'obama', 'stem': 'obama'}, {'count': 20, 'term': 'boehner', 'stem': 'boehner'}, {'count': 4, 'term': 'tax', 'stem': 'tax'}]

答案 1 :(得分:0)

您的密钥存在问题,密钥末尾有,},以解决您可以使用的问题:

import ast
from heapq import nlargest

for k in data.keys():
    k = k[:-3] + ']'
    k = ast.literal_eval(k)
    top_5 = nlargest(5, k, key=lambda i: i["count"])
    print(top_5)

修改

data更新内容后,以下内容应该足够了:

for k in data.keys():
    k = ast.literal_eval(k)
    top_5 = nlargest(5, k, key=lambda i: i["count"])
    print(top_5)

<强>输出:

>>> import ast
>>> from heapq import nlargest
>>>
>>> for k in data.keys():
...     k = ast.literal_eval(k)
...     top_5 = nlargest(5, k, key=lambda i: i["count"])
...     print(top_5)
...
[{'count': 27, 'term': 'obama', 'stem': 'obama'}, {'count': 20, 'term': 'boehner', 'stem': 'boehner'}, {'count': 4, 'term': 'tax', 'stem': 'tax'}, {'count': 3, 'term': 'couldn', 'stem': 'couldn'}, {'count': 3, 'term': 'daley', 'stem': 'daley'}]