简体   繁体   English

根据字典列表中的值获取键

[英]Getting key based on values in list of a dict

I'm trying to get the Key based on values in a list of the key or return the element if the value/key is not found in the dict.我试图根据键列表中的值获取键,或者如果在字典中找不到值/键,则返回元素。

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)

If I try the above, I get the output as:如果我尝试上述操作,我会得到如下输出:

$ python 1.py 
['25 to 29 years', '85 years and over', '65 years and over']

What I require is:我需要的是:

$ python 1.py 
['1+', '25 to 29 years', '85 years and over', '65 years and over']

Thanks in advance for the help在此先感谢您的帮助

these problems are typically easier if you invert your dict如果你反转你的字典,这些问题通常会更容易

inverted_dict = {val:key for key,arr in my_dict.items() for val in arr}

now you can simply lookup your keys现在您可以简单地查找您的密钥

for key in [  '1+', '25_29', '85+', '65+']:
    print(inverted_dict.get(key,key))

This code inverses the dictionary so that each value within the array becomes a new key.此代码反转字典,以便数组中的每个值都成为一个新键。 With that inversed dictionary it's very easy to query individual header keys or fall back to the header name.使用逆字典很容易查询单个标题键或回退到标题名称。

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 to 29 years', '85 years and over', '65 years and over'] ['1+'、'25 至 29 岁'、'85 岁及以上'、'65 岁及以上']

If you can use pandas, you can use this solution:如果你可以使用熊猫,你可以使用这个解决方案:

import pandas as pd

df1 = pd.DataFrame(headersDict, index=[0,1]).T.reset_index()
df1 = pd.DataFrame(pd.concat([df1[0], df1[1]]).drop_duplicates()).join(df1, lsuffix='_1').drop(columns=['0',1]).rename(columns={'0_1':0}) 
a = pd.DataFrame(headersList).merge(df1, 'outer')[0:len(pd.DataFrame(headersList))].set_index(0)['index'] 
a.fillna(a.index.to_series()).values.tolist() 


# ['1+', '25 to 29 years', '85 years and over', '65 years and over']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM