查找值在Python dict中出现的次数

时间:2018-06-11 09:13:03

标签: python json

我正在使用嵌套词典:

CREATE ALGORITHM=UNDEFINED DEFINER=`arturl`@`localhost` SQL SECURITY DEFINER VIEW `view_profit_master`  AS  select `stocktake_locations`.`stocktake_id` AS `stocktake_id`,(select `stocktake_master`.`store_id` from `stocktake_master` where `stocktake_master`.`id` = `stocktake_locations`.`stocktake_id`) AS `store_id`,(select `stocktake_master`.`stock_date` from `stocktake_master` where `stocktake_master`.`id` = `stocktake_locations`.`stocktake_id`) AS `stock_date`,(select sum(`store_purchases`.`cost_value`) from `store_purchases` where `store_purchases`.`stocktake_id` = `stocktake_locations`.`stocktake_id`) AS `total_purchases`,(select sum(`store_wastage`.`wastage_amount`) from `store_wastage` where `store_wastage`.`stocktake_id` = `stocktake_locations`.`stocktake_id`) AS `total_wastage`,(select sum(`store_sales`.`sale_at_cost`) from `store_sales` where `store_sales`.`stocktake_id` = `stocktake_locations`.`stocktake_id`) AS `total_sales`,(select sum(`stocktake_details`.`quantity` * `stocktake_details`.`full_cost` + `stocktake_details`.`quantity_units` * `stocktake_details`.`unit_cost`) from `stocktake_details` where `stocktake_details`.`stocktake_id` = `stocktake_locations`.`stocktake_id`) AS `total_cost` from `stocktake_locations` group by `stocktake_locations`.`stocktake_id` ;

我必须记录次数'博物馆'出现在字典中。我在下面写的代码不打印任何东西,我不知道我错过了什么。是否有另一种方法来迭代dict中的所有值并计算某个值出现的次数?

{
"payload": {
    "existence_full": 1,
    "geo_virtual": "[\"56.9459720|-2.1971226|20|within_50m|4\"]",
    "latitude": "56.945972",
    "locality": "Stonehaven",
    "_records_touched": "{\"crawl\":8,\"lssi\":0,\"polygon_centroid\":0,\"geocoder\":0,\"user_submission\":0,\"tdc\":0,\"gov\":0}",
    "address": "The Lodge, Dunottar",
    "email": "dunnottarcastle@btconnect.com",
    "existence_ml": 0.5694238217658721,
    "domain_aggregate": "",
    "name": "Dunnottar Castle",
    "search_tags": [
        "Dunnottar Castle Aberdeenshire",
        "Dunotter Castle"
    ],
    "admin_region": "Scotland",
    "existence": 1,
    "category_labels": [
        [
            "Landmarks",
            "Buildings and Structures"
        ]
    ],
    "post_town": "Stonehaven",
    "region": "Kincardineshire",
    "review_count": "719",
    "geocode_level": "within_50m",
    "tel": "01569 762173",
    "placerank": 65,
    "longitude": "-2.197123",
    "placerank_ml": 37.27916073464469,
    "fax": "01330 860325",
    "category_ids_text_search": "",
    "website": "http://www.dunnottarcastle.co.uk",
    "status": "1",
    "geocode_confidence": "20",
    "postcode": "AB39 2TL",
    "category_ids": [
        108
    ],
    "country": "gb",
    "_geocode_quality": "4"
},
"uuid": "3867aaf3-12ab-434f-b12b-5d627b3359c3"
}

2 个答案:

答案 0 :(得分:0)

代码museum = {}声明了一个字典。  当您运行代码museum +=1时,它应该给出以下错误:

  

TypeError:+ =的不支持的操作数类型:'dict'和'int'

如果您想要一个计数器,那么请执行museum = 0

之类的操作

答案 1 :(得分:0)

这个怎么样?

import json
museum_count = 0
data = json.load(json_file)
for x in data:
    try:
        for value in data[x]['payload']:
            if value.lower() == "museum":
                museum_count+= 1
    except KeyError, e:
        print str(e)

您的代码错过了KeyError异常。如果你只使用异常,代码就会一直运行(并跳过所有内容),而不会告诉你。正如其他人所说:

museum = {}

声明一个字典,用数字计算或做一些你需要整数的东西。您可以通过例如:

声明一个整数
my_int = 0