将三个列表合并为一个字典

时间:2013-03-29 06:25:33

标签: python list dictionary merge

我需要将三个列表合并到一个字典中。这些列表来自读取我格式化的txt文件,这里是该文件的片段:

maker =['Horsey', 'Ford', 'Overland', 'Scripps-Booth']

year = ['1899', '1909', '1911', '1913']

model = ['Horseless', 'Model T', 'OctoAuto', 'Bi-Autogo']

进入以下内容:

car_dict = {'Horsey':1899,'Horseless','Ford':1909,'Model T','Overland' : 1911, 'OctoAuto', 'Scripps-Booth' : 1913, 'Bi-Autogo'}

这是我做的:

def car_data_merge(car_maker,car_model,car_year):
    car_dict = {}
    car_merge = []

    car_dict = defaultdict(partial(defaultdict,list))

    for (car_maker,car_model,car_year) in zip(car_maker,car_model,car_year):
         car_dict[car_year][car_model].append(car_maker)
    print(car_dict)

当我输入此内容时,我得到:

{'Horsey': defaultdict(<class 'list'>, {'1899': ['Horseless']})

并非显示列表中的所有数据,并且我不希望显示defaultdict

当我尝试以下操作时:

def car_data_merge(car_maker,car_data):
    car_dict = {}
    car_merge = []
    car_merge = zip(car_maker,car_data)  
    car_dict = dict(car_merge)
    print(car_dict)

    ###   car_data holds both year and model   ####

只显示部分数据:

'Horsey':'Horseless',':1909,'Model T

我该怎么办?

4 个答案:

答案 0 :(得分:6)

您使用zip走在正确的轨道上,但请注意:

  

The returned list is truncated in length to the length of the shortest argument sequence.

如果您对此感到满意,可以将数据压缩到元组列表中,压缩密钥,然后将所有内容传递给dict()

如果您想处理缺失的值,请结帐itertools izip_longest (Python 2)zip_longest (Python 3)其中

  

If the iterables are of uneven length, missing values are filled-in with fillvalue.

try:
    # Python 2
    from itertools import izip_longest
    zip_longest = izip_longest
except ImportError:
    # Python 3
    from itertools import zip_longest

from pprint import pprint


def main():
    maker =['Horsey', 'Ford', 'Overland', 'Scripps-Booth', 'FutureX', 'FutureY']
    year = ['1899', '1909', '1911', '1913', '20xx']
    model = ['Horseless', 'Model T', 'OctoAuto', 'Bi-Autogo']

    car_data = dict(zip(maker, zip(year, model)))
    car_data_longest = {mk: (yr, md) for mk, yr, md in zip_longest(maker, year, model)}

    pprint(car_data)
    pprint(car_data_longest)

输出:

{'Ford': ('1909', 'Model T'),
 'Horsey': ('1899', 'Horseless'),
 'Overland': ('1911', 'OctoAuto'),
 'Scripps-Booth': ('1913', 'Bi-Autogo')}
{'Ford': ('1909', 'Model T'),
 'FutureX': ('20xx', None),
 'FutureY': (None, None),
 'Horsey': ('1899', 'Horseless'),
 'Overland': ('1911', 'OctoAuto'),
 'Scripps-Booth': ('1913', 'Bi-Autogo')}

答案 1 :(得分:5)

这是怎么回事:

>>> maker =['Horsey', 'Ford', 'Overland', 'Scripps-Booth']
>>> year = ['1899', '1909', '1911', '1913']
>>> model = ['Horseless', 'Model T', 'OctoAuto', 'Bi-Autogo']
>>> d = dict(zip(maker,zip(year,model)))
{'Overland': ('1911', 'OctoAuto'), 'Horsey': ('1899', 'Horseless'), 'Scripps-Booth': ('1913', 'Bi-Autogo'), 'Ford': ('1909', 'Model T')}

答案 2 :(得分:3)

听起来你想要的是:

{maker: (year, model) for maker, model, year in zip(car_maker,car_model,car_year)}

会给你:

{'Horsey':(1899,'Horseless'),'Ford':(1909,'Model T'),'Overland':(1911,'OctoAuto')...}

答案 3 :(得分:2)

这是你要找的东西吗?

>>> car_dict = {maker[i]:[year[i], model[i]] for i in xrange(len(maker))}
>>> car_dict
{'Overland': ['1911', 'OctoAuto'], 'Horsey': ['1899', 'Horseless'], 'Scripps-Booth': ['1913', 'Bi-Autogo'], 'Ford': ['1909', 'Model T']}