python - 根据字典列表中的值获取键
问题描述
我正在尝试根据键列表中的值获取键,或者如果在字典中找不到值/键,则返回元素。
headersDict = {'Number; SEX AND AGE - Total Population':['TPop'],
'Number; SEX AND AGE - Male Population':['MPop'],
'Number; SEX AND AGE - Female Population':['FPop'],
'Under 5 years': ['<5'],
'5 to 9 years': ['5_9'],
'10 to 14 years': ['10_14'],
'15 to 19 years': ['15_19'],
'20 to 24 years': ['20_24'],
'25 to 29 years': ['25_29'],
'30 to 34 years': ['30_34'],
'35 to 39 years': ['35_39'],
'40 to 44 years': ['40_44'],
'45 to 49 years': ['45_49'],
'50 to 54 years': ['50_54'],
'55 to 59 years': ['55_59'],
'60 to 64 years': ['60_64'],
'65 to 69 years': ['65_69'],
'70 to 74 years': ['70_74'],
'75 to 79 years': ['75_79'],
'80 to 84 years': ['80_84'],
'85 years and over': ['85+'],
'Median age(years)': ['Medage'],
'16 years and over': ['16+'],
'18 years and over': ['18+'],
'21 years and over': ['21+'],
'62 years and over': ['62+', 'sixty two+'],
'65 years and over': ['65+', 'sixty five+']}
headersList = [ '1+', '25_29', '85+',
'65+'
]
new_headersList = [k for k, v in headersDict.items() for elem in headersList for val in v if elem == val]
print(new_headersList)
如果我尝试上述方法,我会得到如下输出:
$ python 1.py
['25 to 29 years', '85 years and over', '65 years and over']
我需要的是:
$ python 1.py
['1+', '25 to 29 years', '85 years and over', '65 years and over']
在此先感谢您的帮助
解决方案
此代码反转字典,以便数组中的每个值都成为一个新键。使用该反向字典,查询单个标题键或回退到标题名称非常容易。
headersDict = {'Number; SEX AND AGE - Total Population': ['TPop'],
'Number; SEX AND AGE - Male Population': ['MPop'],
'Number; SEX AND AGE - Female Population': ['FPop'],
'Under 5 years': ['<5'],
'5 to 9 years': ['5_9'],
'10 to 14 years': ['10_14'],
'15 to 19 years': ['15_19'],
'20 to 24 years': ['20_24'],
'25 to 29 years': ['25_29'],
'30 to 34 years': ['30_34'],
'35 to 39 years': ['35_39'],
'40 to 44 years': ['40_44'],
'45 to 49 years': ['45_49'],
'50 to 54 years': ['50_54'],
'55 to 59 years': ['55_59'],
'60 to 64 years': ['60_64'],
'65 to 69 years': ['65_69'],
'70 to 74 years': ['70_74'],
'75 to 79 years': ['75_79'],
'80 to 84 years': ['80_84'],
'85 years and over': ['85+'],
'Median age(years)': ['Medage'],
'16 years and over': ['16+'],
'18 years and over': ['18+'],
'21 years and over': ['21+'],
'62 years and over': ['62+', 'sixty two+'],
'65 years and over': ['65+', 'sixty five+']}
headersDictReversed = {}
for k, v in headersDict.items():
for new_k in v:
headersDictReversed[new_k] = k
headersList = ['1+', '25_29', '85+', '65+']
results = []
for header in headersList:
# Return the value for header and default to the header itself.
results.append(headersDictReversed.get(header, header))
print(results)
['1+'、'25至29岁'、'85岁及以上'、'65岁及以上']
推荐阅读
- python - Python selenium webdriver:列表索引超出范围最多5个元素
- python - 如果在转换为数据框之前检测到,Python pandas 将删除页眉和页脚记录
- isabelle - 伊莎贝尔:函数没有代码方程
- swiftui - SwiftUI Menu - 菜单实际打开时的操作
- docker - 将docker(compose)图像semver标签记录到fluentd?
- javascript - 在 Google Log 功能之后但在用户登录之前调用 history.push
- python-3.8 - python-AIML 0.9.3 带有日期标签的日期格式
- amazon-web-services - 进行 terraform 更改时,Autoscaling 实例的更改会发生什么变化
- angular - Angular-文件下载的最佳实践
- javascript - ChildProcessError:超出标准输出最大缓冲区长度