python – 用字典中的缩写键替换“全名”键

我有2本词典.

# {state full name: elevation}
elevation = {'Mississippi': 300.0, 'Oklahoma': 1300.0, 'Wyoming': 6700.0, 'Minnesota': 1200.0, 'Illinois': 600.0, 'Arkansas': 650.0, 'New Mexico': 5700.0, 'Ohio': 850.0, 'Indiana': 700.0, 'Maryland': 350.0, 'Louisiana': 100.0, 'Texas': 1700.0, 'Tennessee': 900.0, 'Arizona': 4100.0, 'Iowa': 1100.0, 'Michigan': 900.0, 'Kansas': 2000.0, 'Utah': 6100.0, 'Virginia': 950.0, 'Oregon': 3300.0, 'Connecticut': 500.0, 'Montana': 3400.0, 'California': 2900.0, 'Idaho': 5000.0, 'West Virginia': 1500.0, 'South Carolina': 350.0, 'New Hampshire': 1000.0, 'Massachusetts': 500.0, 'Vermont': 1000.0, 'Georgia': 600.0, 'North Dakota': 1900.0, 'Pennsylvania': 1100.0, 'Florida': 100.0, 'Hawaii': 3030.0, 'Kentucky': 750.0, 'Alaska': 1900.0, 'Nebraska': 2600.0, 'Missouri': 800.0, 'Wisconsin': 1050.0, 'Alabama': 500.0, 'Rhode Island': 200.0, 'South Dakota': 2200.0, 'Colorado': 6800.0, 'New Jersey': 250.0, 'Washington': 1700.0, 'North Carolina': 700.0, 'New York': 1000.0, 'Nevada': 5500.0, 'Delaware': 60.0, 'Maine': 600.0}

# {abbreviation:full name}
postal = {'WA': 'WASHINGTON', 'VA': 'VIRGINIA', 'DE': 'DELAWARE', 'DC': 'DISTRICT OF COLUMBIA', 'WI': 'WISCONSIN', 'WV': 'WEST VIRGINIA', 'HI': 'HAWAII', 'FL': 'FLORIDA', 'FM': 'FEDERATED STATES OF MICRONESIA', 'WY': 'WYOMING', 'NH': 'NEW HAMPSHIRE', 'NJ': 'NEW JERSEY', 'NM': 'NEW MEXICO', 'TX': 'TEXAS', 'LA': 'LOUISIANA', 'NC': 'NORTH CAROLINA', 'ND': 'NORTH DAKOTA', 'NE': 'NEBRASKA', 'TN': 'TENNESSEE', 'NY': 'NEW YORK', 'PA': 'PENNSYLVANIA', 'CA': 'CALIFORNIA', 'NV': 'NEVADA', 'PW': 'PALAU', 'GU': 'GUAM GU', 'CO': 'COLORADO', 'VI': 'VIRGIN ISLANDS', 'AK': 'ALASKA', 'AL': 'ALABAMA', 'AS': 'AMERICAN SAMOA', 'AR': 'ARKANSAS', 'VT': 'VERMONT', 'IL': 'ILLINOIS', 'GA': 'GEORGIA', 'IN': 'INDIANA', 'IA': 'IOWA', 'OK': 'OKLAHOMA', 'AZ': 'ARIZONA', 'ID': 'IDAHO', 'CT': 'CONNECTICUT', 'ME': 'MAINE', 'MD': 'MARYLAND', 'MA': 'MASSACHUSETTS', 'OH': 'OHIO', 'UT': 'UTAH', 'MO': 'MISSOURI', 'MN': 'MINNESOTA', 'MI': 'MICHIGAN', 'MH': 'MARSHALL ISLANDS', 'RI': 'RHODE ISLAND', 'KS': 'KANSAS', 'MT': 'MONTANA', 'MP': 'NORTHERN MARIANA ISLANDS', 'MS': 'MISSISSIPPI', 'PR': 'PUERTO RICO', 'SC': 'SOUTH CAROLINA', 'KY': 'KENTUCKY', 'OR': 'OREGON', 'SD': 'SOUTH DAKOTA'}

我想创建一个新的dic,所以它需要{abbreviation:elevation},而新dict中的键必须同时包含“elevation”和“postal”,如:

{'MS': 300.0, 'OK': 1300.0, 'WY': 6700.0, 'MN': 1200.0, ....}

我写的功能:

def rekeyDict(dct,mapping):
    newDict = {}
    for key in dct.keys():
        try:
            newDict[mapping[key.upper()]] = dct[key]
        except Exception, error:
            print "failed",error
            pass
    return newDict

elevation = rekeyDict(elevation,postal)
print elevation

然而,它给了我一个空白的词.我该如何修复我的代码?

最佳答案 这对你来说很好,只有一行不同.您应该只交换映射字典的键和值.

def rekeyDict(dct,mapping):
    newDict = {}
    # below is different line
    mapping = {v:k for k, v in mapping.items()}
    for key in dct:
        try:
            newDict[mapping[key.upper()]] = dct[key]
        except Exception, error:
            print "failed",error
            pass
    return newDict

elevation = rekeyDict(elevation,postal)
print elevation
点赞