Python中嵌套dict和dict列表的动态迭代方法

Python中嵌套dict和dict列表的动态迭代方法,python,list,python-2.7,dictionary,Python,List,Python 2.7,Dictionary,我正在寻找一种动态的方法来解决我的问题。我有一个非常复杂的结构,但为了简单起见 我的字典结构如下: dict1={ "outer_key1" : { "total" : 5 #1.I want the value of "total" }, "outer_key2" : [{ "type": "ABC", #2. I want to count whole structure

我正在寻找一种动态的方法来解决我的问题。我有一个非常复杂的结构,但为了简单起见

我的字典结构如下:

dict1={
      "outer_key1" : {
      "total" : 5              #1.I want the value of "total" 
      },
      "outer_key2" : 
      [{
       "type": "ABC",          #2. I want to count whole structure where type="ABC"
       "comments": {
       "nested_comment":[
        {
          "key":"value",
          "id": 1
        },      
        {
         "key":"value",
         "id": 2
        } 
       ]                       # 3. Count Dict inside this list.
    }}]}  
我想用这个迭代字典来解#1、#2和#3

我试图解决#1和#3:

输出:

total=5  
2
问题1:在“嵌套注释”列表下获取词典总数的Python方法是什么?
问题2:如何获得type=“ABC”类型的总计数。(注意:type是“outer_key2”下的嵌套键)

问题1:在“嵌套注释”列表下,什么是pythonic方法来获取字典的总数

标准库中的用户
计数器

from collections import Counter

my_list = [{'hello': 'world'}, {'foo': 'bar'}, 1, 2, 'hello']
dict_count = Counter([x for x in my_list if type(x) is dict])

问题2:如何获得type=“ABC”类型的总计数。(注意:type是“outer_key2”下的嵌套键)

不清楚你在这里要什么。如果按“总计数”,则指的是所有目录中的注释总数,其中“类型”等于“ABC”:

但我得说,你处理的是一些奇怪的数据

问题1:在“嵌套注释”列表下,什么是pythonic方法来获取字典的总数

标准库中的用户
计数器

from collections import Counter

my_list = [{'hello': 'world'}, {'foo': 'bar'}, 1, 2, 'hello']
dict_count = Counter([x for x in my_list if type(x) is dict])

问题2:如何获得type=“ABC”类型的总计数。(注意:type是“outer_key2”下的嵌套键)

不清楚你在这里要什么。如果按“总计数”,则指的是所有目录中的注释总数,其中“类型”等于“ABC”:

但我得说,你处理的数据有些奇怪。

你得到了第1和第3题的答案,也检查一下这个

from collections import Counter
dict1={
      "outer_key1" : {
      "total" : 5              #1.I want the value of "total" 
      },
      "outer_key2" : 
      [{
       "type": "ABC",          #2. I want to count whole structure where      type="ABC"
   "comments": {
   "nested_comment":[
    {
      "key":"value",
      "key": "value"
    },      
    {
     "key":"value",
     "id": 2
    } 
   ]                       # 3. Count Dict inside this list.
}}]}


print "total:   ",dict1['outer_key1']['total']
print "No of nested comments:   ", len(dict1['outer_key2'][0]['comments']  ['nested_comment']), 
假设下面是outer_key2的数据结构,这就是如何获得
type='ABC'

dict2={
  "outer_key1" : {
  "total" : 5              
  },
  "outer_key2" : 
  [{
   "type": "ABC",         
   "comments": {'...'}
   },
   {
   "type": "ABC",          
   "comments": {'...'}
   },
   {
   "type": "ABC",         
   "comments": {'...'}
   }]}

i=0
k=0
while k < len(dict2['outer_key2']):
    #print k
    if dict2['outer_key2'][k]['type'] == 'ABC':
        i+=int(1)
    else:
        pass
    k+=1
print ("\r\nNo of dictionaries with type = 'ABC' : "), i
dict2={
“外部按键1”:{
“总数”:5
},
“外部按键2”:
[{
“类型”:“ABC”,
“评论”:{'.'}
},
{
“类型”:“ABC”,
“评论”:{'.'}
},
{
“类型”:“ABC”,
“评论”:{'.'}
}]}
i=0
k=0
而k
你得到了第1题和第3题的答案,也检查一下这个

from collections import Counter
dict1={
      "outer_key1" : {
      "total" : 5              #1.I want the value of "total" 
      },
      "outer_key2" : 
      [{
       "type": "ABC",          #2. I want to count whole structure where      type="ABC"
   "comments": {
   "nested_comment":[
    {
      "key":"value",
      "key": "value"
    },      
    {
     "key":"value",
     "id": 2
    } 
   ]                       # 3. Count Dict inside this list.
}}]}


print "total:   ",dict1['outer_key1']['total']
print "No of nested comments:   ", len(dict1['outer_key2'][0]['comments']  ['nested_comment']), 
假设下面是outer_key2的数据结构,这就是如何获得
type='ABC'

dict2={
  "outer_key1" : {
  "total" : 5              
  },
  "outer_key2" : 
  [{
   "type": "ABC",         
   "comments": {'...'}
   },
   {
   "type": "ABC",          
   "comments": {'...'}
   },
   {
   "type": "ABC",         
   "comments": {'...'}
   }]}

i=0
k=0
while k < len(dict2['outer_key2']):
    #print k
    if dict2['outer_key2'][k]['type'] == 'ABC':
        i+=int(1)
    else:
        pass
    k+=1
print ("\r\nNo of dictionaries with type = 'ABC' : "), i
dict2={
“外部按键1”:{
“总数”:5
},
“外部按键2”:
[{
“类型”:“ABC”,
“评论”:{'.'}
},
{
“类型”:“ABC”,
“评论”:{'.'}
},
{
“类型”:“ABC”,
“评论”:{'.'}
}]}
i=0
k=0
而k