不附加到列表

时间:2013-08-02 18:42:05

标签: python list dictionary

我正在尝试创建一个字典列表,其中每个字典键都是一个作业,每个值都是与该作业相关的能力列表。

例如:

[{'clerk': ['math ability','writing ability',...etc]},{'salesman':['charisma','writing ability','etc']}]

这是我正在使用的数据:

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

这就是我到目前为止所做的:

首先,我创建一个字典列表,每个字典表示上面数据中的一行,其中keys =列名为vals =列值。样本:

OrderedDict([('Domain Source', 'Analyst'), ('Recommend Suppress', 'N'), ('Standard Error', '0.19'), ('Element ID', '1.A.1.a.1'), ('N', '8'), ('Scale ID', 'IM'), ('Not Relevant', 'n/a'), ('Element Name', 'Oral Comprehension'), ('Lower CI Bound', '4.13'), ('Date', '06/2006'), ('Data Value', '4.50'), ('Upper CI Bound', '4.87'), ('O*NET-SOC Code', '11-1011.00')]), OrderedDict([('Domain Source', 'Analyst'), ('Recommend Suppress', 'N'), ('Standard Error', '0.25'), ('Element ID', '1.A.1.a.1'), ('N', '8'), ('Scale ID', 'LV'), ('Not Relevant', 'N'), ('Element Name', 'Oral Comprehension'), ('Lower CI Bound', '4.26'), ('Date', '06/2006'), ('Data Value', '4.75'), ('Upper CI Bound', '5.24'), ('O*NET-SOC Code', '11-1011.00')]), OrderedDict([('Domain Source', 'Analyst'), ('Recommend Suppress', 'N'), ('Standard Error', '0.18'), ('Element ID', '1.A.1.a.2'), ('N', '8'), ('Scale ID', 'IM'), ('Not Relevant', 'n/a'), ('Element Name', 'Written Comprehension'), ('Lower CI Bound', '4.02'), ('Date', '06/2006'), ('Data Value', '4.38'), ('Upper CI Bound', '4.73'), ('O*NET-SOC Code', '11-1011.00')]), OrderedDict([('Domain Source', 'Analyst'), ('Recommend Suppress', 'N'), ('Standard Error', '0.32'), ('Element ID', '1.A.1.a.2'), ('N', '8'), ('Scale ID', 'LV'),

然后我尝试将字典合并到更少的字典中,其中每个键都是作业代码,每个值都是与该作业相关的能力列表。

def add_abilites(abilites_m_l):
    jobs_list = []
    for ind, dict in enumerate(abilites_m_l):
        activities_list = []
        if abilities_m_l[ind-1]['O*NET-SOC Code'] == abilities_m_l[ind]['O*NET-SOC Code']: 
            if abilities_m_l[ind]['Element Name'] != abilities_m_l[ind-1]['Element Name']:
                activities_list.append(abilities_m_l[ind]['Element Name'])
            else: pass
        else: list.append({abilities_m_l[ind]['O*NET-SOC Code']:activities_list})        
    return jobs_list
a_l_with_abilities = add_abilites(abilities_m_l)
print a_l_with_abilities

我得到以下输出:

[{'11-1011.00': []}, {'11-1021.00': []}, {'11-2011.00': []}, {'11-2021.00': []}, {'11-2022.00': []}, {'11-2031.00': []}, {'11-3011.00': []}, {'11-3021.00': []}, {'11-3031.01': []}, {'11-3031.02': []}, {'11-3051.00': []}, {'11-3051.01': []}, {'11-3051.02': []}, {'11-3051.04': []}, {'11-3061.00': []}, {'11-3071.01': []}, {'11-3071.02': []}, {'11-3071.03': []}, {'11-3111.00': []}, {'11-3121.00': []}, {'11-3131.00': []}, {'11-9013.01': []}, {'11-9013.03': []}, {'11-9021.00': []}, {'11-9031.00': []}, {'11-9032.00': []}, {'11-9033.00': []}, {'11-9041.00': []}, {'11-.....

换句话说,我的名单没有填写。

1 个答案:

答案 0 :(得分:1)

核心问题是,您要将activities_list重新分配给abilities_m_l中每个字典的空列表。因此,当您检测到已更改的“O * NET-SOC代码”值时,将附加刚刚重新分配的空列表。

这是一种更简洁的方法:

def add_abilities(abilities_m_l):
    jobs_dict = OrderedDict()
    for data_dict in abilities_m_l:
        o_code = data_dict['O*NET-SOC Code']
        activity = data_dict['Element Name']
        activities_so_far = jobs_dict.setdefault(o_code, OrderedDict())
        activities_so_far[activity] = True
    return [{o_code: activities.keys()} for o_code, activities in jobs_dict.iteritems()]

或者如果您使用的是Python 3,keysvaluesitems调用将返回迭代而不是列表:

    return [{o_code: list(activities.keys())} for o_code, activities in jobs_dict.items()]

或者,如果您不需要保留活动的顺序,请使用set进行活动。这是首选,但遗憾的是,Python不具有原生OrderedSet,所以我在上面用OrderedDict包含True来表示为代码找到的活动。

def add_abilities(abilities_m_l):
    jobs_dict = OrderedDict()
    for data_dict in abilities_m_l:
        o_code = data_dict['O*NET-SOC Code']
        activity = data_dict['Element Name']
        activities_so_far = jobs_dict.setdefault(o_code, set)
        activities_so_far.add(activity)
    return [{o_code: list(activities)} for o_code, activities in jobs_dict.iteritems()]

重点是让Python的词典收集有关共享密钥的信息,并保持每个代码的活动的唯一性。