【发布时间】:2013-11-30 19:23:55
【问题描述】:
我正在使用字典理解来提取嵌套值。我有以下代码(作为示例):
d = {1: 'a', 2: 'b', 3: 'c'}
e = {4: 'd', 5: 'e', 6: 'f'}
f = {7: 'g', 8: 'h', 9: 'i'}
# stick them together into another dict
data_dict = {'one': d, 'two': e, 'three': f}
#say we're looking for the key 8
output = {outer_key: {inner_key: inner_value for inner_key, inner_value in outer_value.items() if inner_key == 8} for outer_key, outer_value in data_dict.items()}
output = {'one': {}, 'three': {8: 'h'}, 'two': {}}
我需要做什么才能获得“三”及其价值?我不想返回空匹配项。
谢谢
【问题讨论】:
标签: python dictionary list-comprehension