在python中从数组中提取值

在python中从数组中提取值,python,arrays,dictionary,key,Python,Arrays,Dictionary,Key,我在访问包含字典和另一个数组的数组中的值时遇到一些问题 看起来是这样的: [{'name': 'Alex', 'number_of_toys': [{'classification': 3, 'count': 383}, {'classification': 1, 'count': 29}, {'classification': 0, 'count': 61}], 'total_toys': 473}, {'name': 'John', 'number_of_toys'

我在访问包含字典和另一个数组的数组中的值时遇到一些问题

看起来是这样的:

[{'name': 'Alex',
  'number_of_toys': [{'classification': 3, 'count': 383},
   {'classification': 1, 'count': 29},
   {'classification': 0, 'count': 61}],
  'total_toys': 473},
 {'name': 'John',
  'number_of_toys': [{'classification': 3, 'count': 8461},
   {'classification': 0, 'count': 3825},
   {'classification': 1, 'count': 1319}],
  'total_toys': 13605}]
我想访问每个“分类”的“计数”编号。例如,对于'name'Alex,如果'classification'为3,则代码返回的'count'为383,对于其他分类和名称,依此类推


谢谢你的帮助

不确定你的问题是什么,但如果只是一个映射练习,这将使你走上正确的轨道

def get_toys(personDict):
    person_toys = personDict.get('number_of_toys')
    return [ (toys.get('classification'), toys.get('count')) for toys in person_toys]

def get_person_toys(database):
    return [(personDict.get('name'), get_toys(personDict)) for personDict in database]
这一结果是:


['Alex',[3383,1,29,0,61],'John',[38461,0,3825,1319]

这不像前面的答案那样优雅,因为它不会迭代值,但如果您想选择特定元素,这是一种方法:

data = [{'name': 'Alex',
  'number_of_toys': [{'classification': 3, 'count': 383},
   {'classification': 1, 'count': 29},
   {'classification': 0, 'count': 61}],
  'total_toys': 473},
 {'name': 'John',
  'number_of_toys': [{'classification': 3, 'count': 8461},
   {'classification': 0, 'count': 3825},
   {'classification': 1, 'count': 1319}],
  'total_toys': 13605}]    

import pandas as pd
df = pd.DataFrame(data)
print(df.loc[0]['name'])
print(df.loc[0][1][0]['classification'])
print(df.loc[0][1][0]['count'])
其中:

Alex
3
383