将字典键映射到值的问题

Posted

技术标签:

【中文标题】将字典键映射到值的问题【英文标题】:Problem with mapping dictionary keys to values 【发布时间】:2020-09-06 23:37:06 【问题描述】:

我有以下命令:

def get_list_of_university_towns():
    import re 
    import pandas as pd 
    dataframe = pd.DataFrame(columns=('State','RegionName'))
    with open('university_towns.txt',"r") as f_in:
        lines = f_in.readlines()
        i = 0 
        for line in lines: 
            if '[edit]' in line:
                 states = re.search(r'^([^(\[]+)', line).group(1)
            else:
                countries = re.search(r'^([^(\[]+)', line).group(1)
                dataframe.loc[i] = [states,countries] 
                i += 1 
        listed = []
        states = 'OH': 'Ohio', 'KY': 'Kentucky', 'AS': 'American Samoa', 'NV': 'Nevada', 'WY': 'Wyoming', 'NA': 'National', 'AL': 'Alabama', 'MD': 'Maryland', 'AK': 'Alaska', 'UT': 'Utah', 'OR': 'Oregon', 'MT': 'Montana', 'IL': 'Illinois', 'TN': 'Tennessee', 'DC': 'District of Columbia', 'VT': 'Vermont', 'ID': 'Idaho', 'AR': 'Arkansas', 'ME': 'Maine', 'WA': 'Washington', 'HI': 'Hawaii', 'WI': 'Wisconsin', 'MI': 'Michigan', 'IN': 'Indiana', 'NJ': 'New Jersey', 'AZ': 'Arizona', 'GU': 'Guam', 'MS': 'Mississippi', 'PR': 'Puerto Rico', 'NC': 'North Carolina', 'TX': 'Texas', 'SD': 'South Dakota', 'MP': 'Northern Mariana Islands', 'IA': 'Iowa', 'MO': 'Missouri', 'CT': 'Connecticut', 'WV': 'West Virginia', 'SC': 'South Carolina', 'LA': 'Louisiana', 'KS': 'Kansas', 'NY': 'New York', 'NE': 'Nebraska', 'OK': 'Oklahoma', 'FL': 'Florida', 'CA': 'California', 'CO': 'Colorado', 'PA': 'Pennsylvania', 'DE': 'Delaware', 'NM': 'New Mexico', 'RI': 'Rhode Island', 'MN': 'Minnesota', 'VI': 'Virgin Islands', 'NH': 'New Hampshire', 'MA': 'Massachusetts', 'GA': 'Georgia', 'ND': 'North Dakota', 'VA': 'Virginia' 
        statesinverse = v: k for k, v in states.items()
        for i in dataframe['State']:
            if i in statesinverse.keys():
                value = statesinverse.get(i)
                listed.append(value)
        dataframe['State'] = listed
    return dataframe
get_list_of_university_towns()

但是,这会以以下形式打印输出:

State   RegionName
0   AL  Auburn
1   AL  Florence
2   AL  Jacksonville

我希望状态显示为 'Alabama' 而不是 'AL'。

因此,我希望字典 'statesinverse' 出现在“State”列中,而不是

有人能帮我一把吗?

【问题讨论】:

你把钥匙颠倒了,你不需要这样做 【参考方案1】:

您不需要反转密钥,dict.get(<key>) 就像 dict[<key>] 并将返回给定密钥的值,但 get() 如果找不到密钥不会出错,将返回 @ 987654324@。确保您使用的数据框传入状态码“AL”:

def get_list_of_university_towns():
    import re 
    import pandas as pd 
    dataframe = pd.DataFrame(columns=('State','RegionName'))
    with open('university_towns.txt',"r") as f_in:
        lines = f_in.readlines()
        i = 0 
        for line in lines: 
            if '[edit]' in line:
                 states = re.search(r'^([^(\[]+)', line).group(1)
            else:
                countries = re.search(r'^([^(\[]+)', line).group(1)
                dataframe.loc[i] = [states,countries] 
                i += 1 
        listed = []
        states = 'OH': 'Ohio', 'KY': 'Kentucky', 'AS': 'American Samoa', 'NV': 'Nevada', 'WY': 'Wyoming', 'NA': 'National', 'AL': 'Alabama', 'MD': 'Maryland', 'AK': 'Alaska', 'UT': 'Utah', 'OR': 'Oregon', 'MT': 'Montana', 'IL': 'Illinois', 'TN': 'Tennessee', 'DC': 'District of Columbia', 'VT': 'Vermont', 'ID': 'Idaho', 'AR': 'Arkansas', 'ME': 'Maine', 'WA': 'Washington', 'HI': 'Hawaii', 'WI': 'Wisconsin', 'MI': 'Michigan', 'IN': 'Indiana', 'NJ': 'New Jersey', 'AZ': 'Arizona', 'GU': 'Guam', 'MS': 'Mississippi', 'PR': 'Puerto Rico', 'NC': 'North Carolina', 'TX': 'Texas', 'SD': 'South Dakota', 'MP': 'Northern Mariana Islands', 'IA': 'Iowa', 'MO': 'Missouri', 'CT': 'Connecticut', 'WV': 'West Virginia', 'SC': 'South Carolina', 'LA': 'Louisiana', 'KS': 'Kansas', 'NY': 'New York', 'NE': 'Nebraska', 'OK': 'Oklahoma', 'FL': 'Florida', 'CA': 'California', 'CO': 'Colorado', 'PA': 'Pennsylvania', 'DE': 'Delaware', 'NM': 'New Mexico', 'RI': 'Rhode Island', 'MN': 'Minnesota', 'VI': 'Virgin Islands', 'NH': 'New Hampshire', 'MA': 'Massachusetts', 'GA': 'Georgia', 'ND': 'North Dakota', 'VA': 'Virginia' 
        for i in dataframe['State']:
            if i in states.keys():
                value = states.get(i) # this should return the value 'Alabama' if key is 'AL'
                listed.append(value)
        dataframe['State'] = listed
    return dataframe
get_list_of_university_towns()

测试一下:

states = 'OH': 'Ohio', 'KY': 'Kentucky', 'AS': 'American Samoa', 'NV': 'Nevada', 'WY': 'Wyoming', 'NA': 'National', 'AL': 'Alabama', 'MD': 'Maryland', 'AK': 'Alaska', 'UT': 'Utah', 'OR': 'Oregon', 'MT': 'Montana', 'IL': 'Illinois', 'TN': 'Tennessee', 'DC': 'District of Columbia', 'VT': 'Vermont', 'ID': 'Idaho', 'AR': 'Arkansas', 'ME': 'Maine', 'WA': 'Washington', 'HI': 'Hawaii', 'WI': 'Wisconsin', 'MI': 'Michigan', 'IN': 'Indiana', 'NJ': 'New Jersey', 'AZ': 'Arizona', 'GU': 'Guam', 'MS': 'Mississippi', 'PR': 'Puerto Rico', 'NC': 'North Carolina', 'TX': 'Texas', 'SD': 'South Dakota', 'MP': 'Northern Mariana Islands', 'IA': 'Iowa', 'MO': 'Missouri', 'CT': 'Connecticut', 'WV': 'West Virginia', 'SC': 'South Carolina', 'LA': 'Louisiana', 'KS': 'Kansas', 'NY': 'New York', 'NE': 'Nebraska', 'OK': 'Oklahoma', 'FL': 'Florida', 'CA': 'California', 'CO': 'Colorado', 'PA': 'Pennsylvania', 'DE': 'Delaware', 'NM': 'New Mexico', 'RI': 'Rhode Island', 'MN': 'Minnesota', 'VI': 'Virgin Islands', 'NH': 'New Hampshire', 'MA': 'Massachusetts', 'GA': 'Georgia', 'ND': 'North Dakota', 'VA': 'Virginia'
states.get('AL')

输出:

'Alabama'

【讨论】:

【参考方案2】:

这部分代码用缩写代替了完整的州名:

listed = []
states = 'OH': 'Ohio', 'KY': 'Kentucky', ... 
statesinverse = v: k for k, v in states.items()
for i in dataframe['State']:
    if i in statesinverse.keys():
        value = statesinverse.get(i)
        listed.append(value)
dataframe['State'] = listed

尝试删除此代码并查看输出。

【讨论】:

非常感谢您的帮助!我的问题已经解决了

以上是关于将字典键映射到值的问题的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript的面向对象

转载java中Map的详解

映射类型--字典

python字典

SQLAlchemy:自动将列名映射到值

地图和字典有啥区别?