2013-03-29 105 views
2

我需要將三個列表合併到一個字典中。這些列表是從閱讀我格式化的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 

我該怎麼辦?

回答

5

你在正確的軌道上與zip,bu牛逼提防:

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

如果你罰款,你可以壓縮你的數據到元組的列表,拉鍊的鑰匙,和手都斷到dict()

如果你想處理缺失值,結賬itertoolsizip_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')} 
+0

問題:我之前從未使用過izip,並且我得到'values = itertools.izip(year,model)' 'AttributeError:'module'object has no attribute'izip''我該怎麼辦? –

+0

@ThomasJones它是'izip_longest' – pradyunsg

+0

@ThomasJones你使用的是什麼版本的Python? 'itertools.izip'是在Python 2.3中添加的,並在Python 2.4中進行了修改。 'itertools.izip_longest'在Python 2.6中是新的。 –

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']} 
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')...} 
4

這個怎麼樣:

>>> 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')} 
+0

你不需要'list'通話。 Zip創建一個元組列表,你可以從中創建一個字典 – Abhijit

+0

@Abhijit糟糕,你是對的。刪除 – TerryA

+0

有人倒投了這個答案;他們能否請澄清爲什麼? – TerryA

相關問題