【问题标题】:3 levels json count in pythonpython中的3级json计数
【发布时间】:2020-05-19 18:33:18
【问题描述】:

我是 python 的新手,我用过其他语言...我用 Java 编写了这段代码并且可以工作,但是现在,我必须用 python 来做。我有一个3级的json,前两个是:resources,usages,我想统计第三级的名字。我看过几个例子,但我无法完成

import json

data = {
  "startDate": "2019-06-23T16:07:21.205Z",
  "endDate": "2019-07-24T16:07:21.205Z",
  "status": "Complete",
  "usages": [
    {
      "name": "PureCloud Edge Virtual Usage",
      "resources": [
        {
          "name": "Edge01-VM-GNS-DemoSite01 (1f279086-a6be-4a21-ab7a-2bb1ae703fa0)",
          "date": "2019-07-24T09:00:28.034Z"
        },
        {
          "name": "329ad5ae-e3a3-4371-9684-13dcb6542e11",
          "date": "2019-07-24T09:00:28.034Z"
        },        
        {
          "name": "e5796741-bd63-4b8e-9837-4afb95bb0c09",
          "date": "2019-07-24T09:00:28.034Z"
        }
      ]
    },
    {
      "name": "PureCloud for SmartVideo Add-On Concurrent",
      "resources": [
        {
          "name": "jpizarro@gns.com.co",
          "date": "2019-06-25T04:54:17.662Z"
        },
        {
          "name": "jaguilera@gns.com.co",
          "date": "2019-06-25T04:54:17.662Z"
        },        
        {
          "name": "dcortes@gns.com.co",
          "date": "2019-07-15T15:06:09.203Z"
        }
      ]
    },
    {
      "name": "PureCloud 3 Concurrent User Usage",
      "resources": [
        {
          "name": "jpizarro@gns.com.co",
          "date": "2019-06-25T04:54:17.662Z"
        },
        {
          "name": "jaguilera@gns.com.co",
          "date": "2019-06-25T04:54:17.662Z"
        },       
        {
          "name": "dcortes@gns.com.co",
          "date": "2019-07-15T15:06:09.203Z"
        }
      ]
    },
    {
      "name": "PureCloud Skype for Business WebSDK",
      "resources": [
        {
          "name": "jpizarro@gns.com.co",
          "date": "2019-06-25T04:54:17.662Z"
        },
        {
          "name": "jaguilera@gns.com.co",
          "date": "2019-06-25T04:54:17.662Z"
        },      
        {
          "name": "dcortes@gns.com.co",
          "date": "2019-07-15T15:06:09.203Z"
        }
      ]
    }
  ],
  "selfUri": "/api/v2/billing/reports/billableusage"
}

cantidadDeLicencias = 0
cantidadDeUsages = len(data['usages'])

for x in range(cantidadDeUsages):
    temporal = data[x]
    cantidadDeResources = len(temporal['resource'])
    for z in range(cantidadDeResources):
        print(x)

我必须做出哪些改变?也许我必须用另一种方法来做?提前致谢

更新 有效的代码

cantidadDeLicencias = 0
for usage in data['usages']:
    cantidadDeLicencias = cantidadDeLicencias + len(usage['resources'])

print(cantidadDeLicencias)

【问题讨论】:

  • 你想知道第三级的计数吗?
  • sum(len(usage['resources']) for use in data['usages'])
  • 这是更好的方法。

标签: python json for-loop


【解决方案1】:

你可以这样做:

for usage in data['usages']:
    print(len(usage['resources']))

【讨论】:

  • 真的!谢谢@ avivoy2006 我将更新我添加的内容并显示总和
【解决方案2】:

如果您想知道每个资源级别中的名称数量,请计算重复名称(例如,“jaguilera@gns.com.co”在您的数据中出现多次) ,然后只需遍历第一级 (usages) 并对每个数组的大小求和

cantidadDeLicencias = 0
for usage in data['usages']:
    cantidadDeLicencias += len(usage['resources'])

print(cantidadDeLicencias)

如果您不想计算重复项,则使用一个集合并遍历每个 resources 数组

cantidadDeLicencias_set = {}
for usage in data['usages']:
    for resource in usage['resources']:
        cantidadDeLicencias_set.add(resource['name'])

print(len(cantidadDeLicencias_set ))

【讨论】:

    猜你喜欢
    • 2017-03-26
    • 2017-12-29
    • 1970-01-01
    • 2019-10-19
    • 2017-01-19
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多