【问题标题】:Iterate over Nested dictionary with similar keys but different values迭代具有相似键但不同值的嵌套字典
【发布时间】:2014-05-08 11:29:01
【问题描述】:
    {
       "matchesPlayed":{"label":"Matches Played","value":7}

      , "matches":[
    {
      "date":"21 Jun"
      ,"hteamcode":"ESP"
      ,"hteamName":"Spain"
      ,"ateamcode":"HON"
      ,"ateamName":"Honduras"

      ,"score":"2:0 (1:0)"

    }
  ,
    {
      "date":"25 Jun"
      ,"hteamcode":"CHI"
      ,"hteamName":"Chile"
      ,"ateamcode":"ESP"
      ,"ateamName":"Spain"

      ,"score":"1:2 (0:2)"

    }
  ,
    {
      "date":"07 Jul"
      ,"hteamcode":"GER"
      ,"hteamName":"Germany"
      ,"ateamcode":"ESP"
      ,"ateamName":"Spain"


    }
  ,
    {
      "date":"29 Jun"
      ,"hteamcode":"ESP"
      ,"hteamName":"Spain"
      ,"ateamcode":"POR"
      ,"ateamName":"Portugal"

      ,"score":"1:0 (0:0)"

    }
  ,
    {
      "date":"11 Jul"
      ,"hteamcode":"NED"
      ,"hteamName":"Netherlands"
      ,"ateamcode":"ESP"
      ,"ateamName":"Spain"

      ,"score":"0:1 a.e.t."


    }
  ,
    {
      "date":"03 Jul"
      ,"hteamcode":"PAR"
      ,"hteamName":"Paraguay"
      ,"ateamcode":"ESP"
      ,"ateamName":"Spain"

      ,"score":"0:1 (0:0)"

    }
  ,
    {
      "date":"16 Jun"
      ,"hteamcode":"ESP"
      ,"hteamName":"Spain"
      ,"ateamcode":"SUI"
      ,"ateamName":"Switzerland"

      ,"score":"0:1 (0:0)"

    }
  ]
    }

这是我的 Json 文件的副本。我如何遍历 json 并制作一个列表/或打印一个特定的键(例如:“hteamName”),这在不同的数组中是相似的,但具有不同的值。 从上一个问题中,我得到了一些可以遍历嵌套字典的代码,但它只能找到具有唯一值的唯一键。

def search(nest, nestitems):
    found = []
    for key, value in nest.iteritems():
        if key == nestitems:
            found.append(value)
        elif isinstance(value, dict):
            found.extend(search(value, nestitems))
        elif isinstance(value, list):
            for item in value:
                if isinstance(item, dict):
                    found.extend(search(item, nestitems))

        else:
            if key == nestitems:
                found.append(value)
    return found 

【问题讨论】:

  • 这个问题是关于什么编程语言的?
  • 您能否提供一个您想要获得的示例输出?用于搜索功能的此输入和关键字。
  • "hteamName":"Spain", "hteamName":"Chile", "hteamName":"Germany", "hteamName":"Spain", "hteamName":"Netherlands", "hteamName ":"巴拉圭", "hteamName":"西班牙"
  • 另外,我正在从 json 文件加载 json 数组,并将 json 文件加载到 python 对象中 - json_data=open('C:/Json/Spain.json') data = json.load(json_data)

标签: python json search dictionary


【解决方案1】:

试试这个更简单的版本:

with open(r'C:/Json/Spain.json') as f:
    data = json.load(f)

for match in data['matches']:
    print(match['hteamName'])

它应该可以帮助您开始其余的工作。

【讨论】:

  • 谢谢,但实际上我正在尝试提取键值对来绘制图形节点,而不仅仅是打印它们。所以要么我必须列出一个列表,要么遍历整个 json 文件(使用循环)并自动选择键、值对
  • 谢谢,这让我开始了。
【解决方案2】:
["hteamName :"+ x.get("hteamName") for x in d["matches"] ]
['hteamName :Spain', 'hteamName :Chile', 'hteamName :Germany', 'hteamName :Spain',    'hteamName :Netherlands', 'hteamName :Paraguay', 'hteamName :Spain']

在函数中:

def search(d, k,nested):
    result=[]
    for n in d[nested]:
        result.append('{0} : {1}'.format(k,n.get(k)))
    return result

search(d,"hteamName","matches")
['hteamName : Spain', 'hteamName : Chile', 'hteamName : Germany', 'hteamName : Spain', 'hteamName : Netherlands', 'hteamName : Paraguay', 'hteamName : Spain']

这应该可以帮助您入门。

【讨论】:

  • 谢谢,但实际上我正在尝试提取键值对来绘制图形节点,而不仅仅是打印它们。所以要么我必须列出一个列表,要么遍历整个 json 文件(使用循环)并自动选择键、值对
  • 我在 Search@(d,'hteamName', 'matches') 遇到语法错误,但第一个运行良好
  • @TheSolider。抱歉,错过了结束],现在应该可以使用了
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 2023-03-02
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多