Merge three list to Dictionary but everything is out of place/not printed

泪湿孤枕 提交于 2019-12-11 14:26:34

问题


I asked this question yesterday

Merging three lists into into one dictionary

The number one answer was the most correct in what I needed

but re-looking at MY automobile.txt file I realized two things.

  1. It has multiple car makers in the list and only one of each is listed.

  2. Every data seems to be out place but a few.

I'm so close but so far.

Here is what I Have

Please Note if it makes a difference This is being read from a .txt file I appended and split the data into three list. I know the data is there. I checked.

from pprint import pprint
import string
import re

data_file = open("Automobil.txt", 'r')
try:
    from itertools import izip_longest
    zip_longest = izip_longest
except ImportError:
    from itertools import zip_longest

def formatting_list(data_file):
    year = []
    maker = []
    model = []
    for line in data_file:
        maker = (' '.join(line.split()[1:2]))
        year =  (' '.join(line.split()[0:1]))
        model = ((' '.join(line.split()[2:])))
    return maker,year,model

def car_data_merge(maker,year,model):
#This was the one suggested to me that I liked

     car_dict_longest = {mk: (yr, md) for mk, yr, md in zip_longest(maker,year,model)}
     pprint(car_dict_longest)

This is something similar to what I tried that another person showed me on the other page

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

After reading up on the uses of izip and how it is different from zip

I thought izip kept everything on place instead of it being temporary tuple.

This a small part of the .txt list where I think the problem is:

year = [1958,1909,1958,1958, 1961, 1961]

maker = [Ford,Ford,Lotus,MGA, Amphicar, Corvair]

model = [Edsel,Model T,Elite,Twin Cam]

When ran this is what should happen

Ford: {1958 , Edsel}, Ford: {1909, Model T}, Lotus: {1958, Elite}, MGA :{1958, Twin Cam}, Amphicar:{1961}, Corvair:{1961}

Instead I get something like:

Ford: {1958 , Edsel}, Lotus: {1958, MGA}, Amphicar:{1961, Corvair}

Any help or guidance would be appreciated.


回答1:


Recommendation

You could do a dictionary of lists of tuples. Defaultdict is your friend.

Code

from collections import defaultdict

year = [1958, 1909, 1958, 1958, 1961, 1961]
maker = ['Ford', 'Ford', 'Lotus', 'MGA', 'Amphicar', 'Corvair']
model = ['Edsel', 'Model T', 'Elite', 'Twin Cam', '', '']

d = defaultdict(list)
for maker, model, year in zip(maker, model, year):
    d[maker].append((model, year))

Result

>>> from collections import defaultdict
>>> 
>>> year = [1958, 1909, 1958, 1958, 1961, 1961]
>>> maker = ['Ford','Ford','Lotus','MGA', 'Amphicar', 'Corvair']
>>> model = ['Edsel', 'Model T', 'Elite', 'Twin Cam', "", ""]
>>> 
>>> d = defaultdict(list)
>>> for maker, model, year in zip(maker, model, year):
...     d[maker].append((model, year))
... 
>>> from pprint import pprint
>>> pprint(dict(d))
{'Amphicar': [('', 1961)],
 'Corvair': [('', 1961)],
 'Ford': [('Edsel', 1958), ('Model T', 1909)],
 'Lotus': [('Elite', 1958)],
 'MGA': [('Twin Cam', 1958)]}

You can also use your izip_longest:

from itertools import izip_longest

...

d = defaultdict(list)
for maker, model, year in izip_longest(maker, model, year):
    d[maker].append((model, year))

Then you get None for the other values.



来源:https://stackoverflow.com/questions/15713825/merge-three-list-to-dictionary-but-everything-is-out-of-place-not-printed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!