我正在学习Python上的嵌套字典。我想知道如何从嵌套字典中提取特定信息。 我的数据如下:
data = {
"1": {'area': 'Administration', 'expenditure': '315'},
"2": {'area': 'Administration', 'expenditure': '120'},
"3": {'area': None, 'expenditure': '314'},
"4": {'area': 'Aids and appliances', 'expenditure': None},
"5": {'area': 'Aids and appliances', 'expenditure': '12'},
"6": {'area': 'Administration', 'expenditure': '110'},
"7": {'area': 'Administration', 'expenditure': '300'},
}
如何提取每个领域的所有支出信息? 任何帮助表示赞赏。
答案 0 :(得分:0)
要查找expenditure
键的值:
for findvals in data.values():
for k, v in findvals.items():
if k == "expenditure":
print(v)
### this prints all the values for key equal to "expenditure"
答案 1 :(得分:0)
使用collections.defaultdict
。创建一个字典,其中面积作为键,支出作为列表中的值。
from collections import defaultdict
data = {
"1": {'area': 'Administration', 'expenditure': '315'},
"2": {'area': 'Administration', 'expenditure': '120'},
"3": {'area': None, 'expenditure':'314'},
"4":{'area': 'Aids and appliances', 'expenditure': None} ,
"5":{'area': 'Aids and appliances', 'expenditure': '12'},
"6":{'area': 'Administration', 'expenditure': '110'},
"7":{'area': 'Administration', 'expenditure': '300'}
}
storage = defaultdict(list)
# defaultdict allows to define keys on fly
{storage[d['area']].append(d['expenditure']) for _, d in data.items() if d['area']}
# we have append all expenditure to all areas where the area is not None.
print(storage)
# to go back to normal dictionary with list of expenditures
results = dict(storage)
print(results)
# outcome: {'Administration': ['315', '120', '110', '300'], 'Aids and appliances': [None, '12']}
如果您不熟悉列表理解,则可以使用常规的for循环:
# we can re-write this:
{storage[d['area']].append(d['expenditure']) for _, d in data.items() if d['area']}
# to
for _, d in data.items():
if d['area']:
storage[d['area']].append(d['expenditure'])
# get all items, and add them to storage if area is not False.
为了效率,我更喜欢第一,但为了提高可读性,您可以第二。
答案 2 :(得分:0)
尝试
[val['expenditure'] for val in data.values() if val['area'] in ['Administration', 'Aids and appliances']]
答案 3 :(得分:0)
您将字典写错了(没有注释和错字!)
data =
{'1': {'area': 'Administration', 'expenditure': '315'},
'2': {'area': 'Administration', 'expenditure': '120'},
'3': {'area': None, 'expenditure': '314'},
'4': {'area': 'Aids and appliances', 'expenditure': None},
'5': {'area': 'Aids and appliances', 'expenditure': '12'}}
只需执行以下操作:
[[(v.get("area", None),y) for x,y in v.items() if x=='expenditure'] for k,v in data.items()]
答案 4 :(得分:0)
我认为您想在expenditure
上使用特定的specific areas
。如果您需要所有区域,则只需从我的代码中删除一个条件,否则您可以引用所有其他答案。
我按照用户输入的任何区域名称来执行此操作,他将获得该特定expenditures
中的area
。
def findExpense(data):
a = input("Enter area to find expenditure : ")
ex_dict = {}
# create expenditure list as values to ex_dict
ex_dict[a] = []
for _,vals in data.items():
if vals['area'] == a:
ex_dict[a].append(vals['expenditure'])
return ex_dict
print(findExpense({
"1": {'area': 'Administration', 'expenditure': '315'},
"2": {'area': 'Administration', 'expenditure': '120'},
"3": {'area': None, 'expenditure': '314'},
"4": {'area': 'Aids and appliances', 'expenditure': None},
"5": {'area': 'Aids and appliances', 'expenditure': '12'},
"6": {'area': 'Administration', 'expenditure': '110'},
"7": {'area': 'Administration', 'expenditure': '300'}
}))
如果您熟悉list comprehension
,则可以用以下代码替换for
循环。
ex_dict[a] = [vals['expenditure'] for _, vals in data.items() if vals['area'] == a]
return ex_dict