如何使用字典理解从已知值获取密钥

时间:2019-07-10 15:00:19

标签: key python-3.6 dictionary-comprehension

假设我有一个变量import pandas as pd a = pd.Series(['EY', 'BAIN', 'KPMG', 'EY']) b = pd.Series([' 10wow this is terrible data8 ', '10/ USED TO BE ANOTHER NUMBER/ 2', ' OMG 106 OMG ', ' 10?7']) y = pd.Series(['BAIN', 'KPMG', 'EY', 'EY' ]) z = pd.Series([108, 102, 106, 107 ]) goal = pd.DataFrame shortdf = pd.DataFrame({'consultant': a, 'invoice_number':b}) longdf = shortdf.copy(deep=True) goal = pd.DataFrame({'consultant': y, 'invoice_number':z}) shortinvoice = shortdf['invoice_number'] longinvoice = longdf['invoice_number'] frames = [shortinvoice, longinvoice] new_list=[] for eachitemer in frames: eachitemer.str.extract('(\d+)').astype(float) #extracing all numbers in the df cell eachitemer.str.strip() #strip the blank/whitespaces in between the numbers new_list.append(eachitemer) new_short_df = new_list[0] new_long_df = new_list[1] ,可以在字典中找到它:

state = "MD"

我想要一个快速的函数,该函数使用字典理解功能基于stateabb = {'Alabama': 'AL','Alaska': 'AK','Arizona': 'AZ','Arkansas': 'AR','California': 'CA','Colorado': 'CO','Connecticut': 'CT','Delaware': 'DE','DistrictOfColumbia': 'DC','Florida': 'FL', 'Georgia': 'GA','Hawaii': 'HI','Idaho': 'ID','Illinois': 'IL','Indiana': 'IN','Iowa': 'IA','Kansas': 'KS','Kentucky': 'KY','Louisiana': 'LA','Maine': 'ME','Maryland': 'MD', 'Massachusetts': 'MA','Michigan': 'MI','Minnesota': 'MN','Mississippi': 'MS','Missouri': 'MO','Montana': 'MT','Nebraska': 'NE','Nevada': 'NV','NewHampshire': 'NH', 'NewJersey': 'NJ','NewMexico': 'NM','NewYork': 'NY','NorthCarolina': 'NC','NorthDakota': 'ND','Ohio': 'OH','Oklahoma': 'OK','Oregon': 'OR','Pennsylvania': 'PA', 'RhodeIsland': 'RI','SouthCarolina': 'SC','SouthDakota': 'SD','Tennessee': 'TN','Texas': 'TX','Utah': 'UT','Vermont': 'VT','Virginia': 'VA','Washington': 'WA', 'WestVirginia': 'WV','Wisconsin': 'WI','Wyoming': 'WY'} 获取匹配的key['stateName']并将其分配给一个空变量:

value['state']

注意:这不太正确;我在这里做什么错了?

2 个答案:

答案 0 :(得分:1)

您对自己的问题的回答是正确的,但不是最佳的。每次您想要通过缩写获取状态名称时,都要遍历状态/缩写对,直到找到期望的缩写(时间复杂度为O(n),其中n是状态数)。由于按状态只有一个且只有一个缩写,因此很容易反转字典:

>>> stateabb = {'Alabama': 'AL','Alaska': 'AK','Arizona': 'AZ','Arkansas': 'AR','California': 'CA','Colorado': 'CO','Connecticut': 'CT','Delaware': 'DE','DistrictOfColumbia': 'DC','Florida': 'FL', 'Georgia': 'GA','Hawaii': 'HI','Idaho': 'ID','Illinois': 'IL','Indiana': 'IN','Iowa': 'IA','Kansas': 'KS','Kentucky': 'KY','Louisiana': 'LA','Maine': 'ME','Maryland': 'MD', 'Massachusetts': 'MA','Michigan': 'MI','Minnesota': 'MN','Mississippi': 'MS','Missouri': 'MO','Montana': 'MT','Nebraska': 'NE','Nevada': 'NV','NewHampshire': 'NH', 'NewJersey': 'NJ','NewMexico': 'NM','NewYork': 'NY','NorthCarolina': 'NC','NorthDakota': 'ND','Ohio': 'OH','Oklahoma': 'OK','Oregon': 'OR','Pennsylvania': 'PA', 'RhodeIsland': 'RI','SouthCarolina': 'SC','SouthDakota': 'SD','Tennessee': 'TN','Texas': 'TX','Utah': 'UT','Vermont': 'VT','Virginia': 'VA','Washington': 'WA', 'WestVirginia': 'WV','Wisconsin': 'WI','Wyoming': 'WY'}
>>> state_name_by_abb = {v: k for k, v in stateabb.items()}
>>> state_name_by_abb
{'AL': 'Alabama', 'AK': 'Alaska', 'AZ': 'Arizona', 'AR': 'Arkansas', 'CA': 'California', 'CO': 'Colorado', 'CT': 'Connecticut', 'DE': 'Delaware', 'DC': 'DistrictOfColumbia', 'FL': 'Florida', 'GA': 'Georgia', 'HI': 'Hawaii', 'ID': 'Idaho', 'IL': 'Illinois', 'IN': 'Indiana', 'IA': 'Iowa', 'KS': 'Kansas', 'KY': 'Kentucky', 'LA': 'Louisiana', 'ME': 'Maine', 'MD': 'Maryland', 'MA': 'Massachusetts', 'MI': 'Michigan', 'MN': 'Minnesota', 'MS': 'Mississippi', 'MO': 'Missouri', 'MT': 'Montana', 'NE': 'Nebraska', 'NV': 'Nevada', 'NH': 'NewHampshire', 'NJ': 'NewJersey', 'NM': 'NewMexico', 'NY': 'NewYork', 'NC': 'NorthCarolina', 'ND': 'NorthDakota', 'OH': 'Ohio', 'OK': 'Oklahoma', 'OR': 'Oregon', 'PA': 'Pennsylvania', 'RI': 'RhodeIsland', 'SC': 'SouthCarolina', 'SD': 'SouthDakota', 'TN': 'Tennessee', 'TX': 'Texas', 'UT': 'Utah', 'VT': 'Vermont', 'VA': 'Virginia', 'WA': 'Washington', 'WV': 'WestVirginia', 'WI': 'Wisconsin', 'WY': 'Wyoming'}

然后在O(1)时间中按其缩写找到任何州名:

>>> state_name_by_abb["HI"]
'Hawaii'

除了考虑时间复杂度之外,(对我来说)这种方法似乎更易于理解。

答案 1 :(得分:0)

对不起,答案很简单。我只是稍作调整并添加一个if语句:

def long_name(stAbbrev):
stateName = [k for k, v in stateabb.items() if v == stAbbrev]
return stateName