计算JSON元素中项的出现次数

时间:2015-03-02 21:33:33

标签: python json

我使用Python来解析英国警察API。我想要的是分析我得到的JSON响应,以计算某次攻击发生的次数。这是API的响应示例。

{
    category: "anti-social-behaviour",
    location_type: "Force",
    location: {
        latitude: "53.349920",
        street: {
            id: 583315,
            name: "On or near Evenwood Close"
        },
        longitude: "-2.657889"
    },
    context: "",
    outcome_status: null,
    persistent_id: "",
    id: 22687179,
    location_subtype: "",
   month: "2013-03"
},

使用此代码

from json import load
from urllib2 import urlopen
import json

url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03"
json_obj = urlopen(url)
player_json_list = load(json_obj)

for player in player_json_list:
    crimeCategories = json.dumps(player['category'], indent = 2, separators=(',', ': '))
    print crimeCategories

我收到了这样的回复

"anti-social-behaviour"
"anti-social-behaviour"
"anti-social-behaviour"
"anti-social-behaviour"
"drugs"
"drugs"
"burglary"

如果我将for循环更改为

for player in player_json_list:
    crimeCategories = json.dumps(player['category'], indent = 2, separators=(',', ': '))
    print crimeCategories.count("drugs")

然后我得到像

这样的回复
0
0
0
0
1
1
0

在论坛上搜索几个小时并没有帮助我!有什么想法吗?

4 个答案:

答案 0 :(得分:10)

您可以将collections.Counter dict与请求结合使用,这些请求将成为一些简洁的代码行:

import  requests
from collections import Counter

url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03"
json_obj = requests.get(url).json()

c = Counter(player['category'] for player in json_obj)
print(c)

输出:

Counter({'anti-social-behaviour': 79, 'criminal-damage-arson': 12, 'other-crime': 11, 'violent-crime': 9, 'vehicle-crime': 7, 'other-theft': 6, 'burglary': 4, 'public-disorder-weapons': 3, 'shoplifting': 2, 'drugs': 2})

如果您更喜欢正常的词典,那么只需在Counter dict上调用dict:

from pprint import pprint as pp
c = dict(c)
pp(c)
{'anti-social-behaviour': 79,
 'burglary': 4,
 'criminal-damage-arson': 12,
 'drugs': 2,
 'other-crime': 11,
 'other-theft': 6,
 'public-disorder-weapons': 3,
 'shoplifting': 2,
 'vehicle-crime': 7,
 'violent-crime': 9}

然后您只需按键c['drugs']等进行访问。

或者遍历这些项目以打印犯罪并以您想要的格式计算:

for k, v in c.items():
    print("{} count:  {}".format(k, v)

输出:

drugs count:  2
shoplifting count:  2
other-theft count:  6
anti-social-behaviour count:  79
violent-crime count:  9
criminal-damage-arson count:  12
vehicle-crime count:  7
public-disorder-weapons count:  3
other-crime count:  11
burglary count:  4

答案 1 :(得分:0)

创建字典并使用crimeCategories作为键。对于值,请使用整数。尝试将这样的东西放在你的循环中。

>>> count['testing'] = count.get('testing',0) + 1
>>> count['testing']
1

答案 2 :(得分:0)

您无法在任何地方存储您的计数。基本上你只需要调用循环中当前项目的计数。

您需要将每个项目添加为字典中的键,然后每次在for循环中遇到某个项目时都会增加值

adictionary = {"drugs":0}
for player in player_list:
  if adictionary.category
    adictionary.category += 1
print adictionary.category

答案 3 :(得分:0)

您可以将数据汇总到category-> json的映射中,如下所示:

from collections import defaultdict

players_by_category = defaultdict(list)
for player in players_json_list:
    players_by_category[player['category'].append(player)

现在你有一本字典,其中列出了每个类别下的犯罪列表。

因此,要获得某种类别的罪行,您需要的是:

for k, v in players_by_category.iteritems():
    print "%s: %s" (k, len(v))

使用player代替crime非常令人困惑,但无论你认为合适,都是如此:)