Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.x中打印嵌套字典中的值_Python_Json_Python 2.7_Dictionary_Nested - Fatal编程技术网

在Python2.x中打印嵌套字典中的值

在Python2.x中打印嵌套字典中的值,python,json,python-2.7,dictionary,nested,Python,Json,Python 2.7,Dictionary,Nested,我在Python 2.x脚本中返回了一个字典类型变量,该变量包含以下值:- {u'image': u'/users/me/Desktop/12345_507630509400555_869403269181768452_n.jpg', u'faces': [{u'gender': {u'gender': u'FEMALE', u'score': 0.731059}, u'age': {u'max': 17, u'score': 0.983185}, u'face_location': {u'wi

我在Python 2.x脚本中返回了一个字典类型变量,该变量包含以下值:-

{u'image': u'/users/me/Desktop/12345_507630509400555_869403269181768452_n.jpg', u'faces': [{u'gender': {u'gender': u'FEMALE', u'score': 0.731059}, u'age': {u'max': 17, u'score': 0.983185}, u'face_location': {u'width': 102, u'top': 102, u'left': 426, u'height': 106}}]}
我要做的是为给定的键提取以下值:-

  • “性别”(值为“女性”)
  • “分数”(值为“0.731059”)
  • “年龄”[max](值为“17”)
  • “年龄”[分数](值为“0.983185”)
我尝试了下面的方法,但似乎没有返回我想要的:

      if key == 'faces':                      
          for k, v in key:                    
              print(k['gender'], k['max'], k['age'][0], k['age'][1])    

关于如何访问和打印我感兴趣的值,有什么建议吗

您有嵌套的目录和列表:

d = {u'image': u'/users/me/Desktop/12345_507630509400555_869403269181768452_n.jpg', u'faces': [{u'gender': {u'gender': u'FEMALE', u'score': 0.731059}, u'age': {u'max': 17, u'score': 0.983185}, u'face_location': {u'width': 102, u'top': 102, u'left': 426, u'height': 106}}]}

# iterate over the list of dict(s)
for dct in d["faces"]:
    gender, age = dct['gender'], dct["age"]
    print(gender["gender"], gender["score"], age["max"], age["score"])
性别格言看起来像:

{u'gender': u'FEMALE', u'score': 0.731059}
 {u'max': 17, u'score': 0.983185}
因此,我们使用键“性别”和“分数”来获取值,年龄dict如下所示:

{u'gender': u'FEMALE', u'score': 0.731059}
 {u'max': 17, u'score': 0.983185}

同样,我们只需使用键“max”和“score”来获取值

您有嵌套的dict和列表:

d = {u'image': u'/users/me/Desktop/12345_507630509400555_869403269181768452_n.jpg', u'faces': [{u'gender': {u'gender': u'FEMALE', u'score': 0.731059}, u'age': {u'max': 17, u'score': 0.983185}, u'face_location': {u'width': 102, u'top': 102, u'left': 426, u'height': 106}}]}

# iterate over the list of dict(s)
for dct in d["faces"]:
    gender, age = dct['gender'], dct["age"]
    print(gender["gender"], gender["score"], age["max"], age["score"])
性别格言看起来像:

{u'gender': u'FEMALE', u'score': 0.731059}
 {u'max': 17, u'score': 0.983185}
因此,我们使用键“性别”和“分数”来获取值,年龄dict如下所示:

{u'gender': u'FEMALE', u'score': 0.731059}
 {u'max': 17, u'score': 0.983185}

同样,我们只需使用“max”和“score”键获取值,这有点复杂。这就是提取所需值的方法:

d
成为你的命令:

key = 'faces'
inner = d[key][0]
print(inner['gender']['gender'], inner['gender']['score'], inner['age']['max'], inner['age']['score']) 
输出:

FEMALE 0.731059 17 0.983185

这有点复杂。这是提取所需值的方法:

d
成为你的命令:

key = 'faces'
inner = d[key][0]
print(inner['gender']['gender'], inner['gender']['score'], inner['age']['max'], inner['age']['score']) 
输出:

FEMALE 0.731059 17 0.983185

完美的谢谢。谢谢你的解释和回答。谢谢。谢谢你的解释和回答