我正在使用理解来创建字典。该错误表明我的密钥存储为值而不是密钥。
这是完整的情况:
我的 BLS 数据如下所示:
O*NET-SOC Code Element ID Element Name Scale ID Data Value N Standard Error Lower CI Bound Upper CI Bound Recommend Suppress Not Relevant Date Domain Source
11-1011.00 1.A.1.a.1 Oral Comprehension IM 4.5 8 0.19 4.13 4.87 N n/a Jun-06 Analyst
11-1011.00 1.A.1.a.1 Oral Comprehension LV 4.75 8 0.25 4.26 5.24 N N Jun-06 Analyst
11-1011.00 1.A.1.a.2 Written Comprehension IM 4.38 8 0.18 4.02 4.73 N n/a Jun-06 Analyst
并希望返回一个看起来像这样的结构,其中包含每个作业的详细级别数据(作业由 SOC 代码表示,数据的最左侧列。例如,作业“职员”的 SOC 代码下面可能是 11-1011.00)。
{
'job':'clerk', 'description': 'works in a bank', 'abilities': {
'math': [
{
'Scale ID': 'IM',
'Data Value': 4.5,
'N' : 8,
'S_E': 0.19
},
{'Scale ID': 'LV',
'Data Value': 4.75,
'N': 8,
'S_E': 0.25
}
]
'english': [
{
'Scale ID': 'IM',
'Data Value': 2,
'N' : 8,
'S_E': 0.19
},
{'Scale ID': 'LV',
'Data Value': 3,
'N': 8,
'S_E': 0.25
}
]
}
},
为此,我在我的能力字段中创建了一个字典列表,并为每个能力创建一个级别 (LV) 和重要性 (IM) 数据列表。
当我为 IM / LV 字典创建字典时,会标记该错误。
这是我的代码:
"""
Abilities
"""
# returns a list of dictionaries, each of the dictionaries representing a row in the data posted above with keys = top row and values = row vals.
abilities_m_l = create_abilities_master_list(file_list[0])
# for each job (for each dictionary in the master list of job dictionaries)
for job in d_l:
job_row = 1
while job_row < 100: # just to test
# initialize the list of abilities for the job
job['abilities'] = []
job['abilities'].append({abilities_m_l[job_row]['Element Name']:[]})
while job['O*NET-SOC Code'] == abilities_m_l[job_row]['O*NET-SOC Code']:
if abilities_m_l[job_row]['Element Name'] == abilities_m_l[job_row-1]['Element Name']:
job[abilities_m_l[job_row]['Element Name']].append({key:value for key,value in abilities_m_l[job_row] if key not in ('O*NET-SOC Code','Element Name', 'Scale ID')})
else:
job[abilities_m_l[job_row]['Element Name']] = []
job[abilities_m_l[job_row]['Element Name']].append({key:value for key,value in abilities_m_l[job_row] if key not in ('O*NET-SOC Code','Element Name', 'Scale ID')})
job_row += 1
break
for job in d_l:
print job['abilities']
这是我的错误:
Traceback (most recent call last):
File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/bls-397147690.633.py", line 65, in <module>
job[abilities_m_l[job_row]['Element Name']].append({key:value for key,value in abilities_m_l[job_row] if key not in ('O*NET-SOC Code','Element Name', 'Scale ID')})
KeyError: 'Oral Comprehension'
logout