Hello,

I'm currently working on a homework problem that requires me to create a dictionary from a .txt file that contains some of the worst cars ever made. The file looks something like this:

1958 MGA Twin Cam
1958 Zunndapp Janus
1961 Amphicar
1961 Corvair
1966 Peel Trident
1970 AMC Gremlin
1970 Triumph Stag
1971 Chrysler Imperial LeBaron Two-Door Hardtop

The car manufacturer should be the key and a tuple containing the year and the model should be the key's value. I tried the following to just get the contents of the file into a list, but only the very last line in the txt file is shown as a list with three elements (ie, ['2004', 'Chevy', 'SSR']) when I print temp.

d={}
car_file = open('worstcars.txt', 'r')
for line in car_file:
    temp = line.split()


print (temp)
car_file.close()

After playing around with the code, I came up with the following code to get everything into a list:

d=[]
car_file = open('worstcars.txt', 'r')
for line in car_file:
    d.append(line.strip('\n'))


print (d)
car_file.close()

Every line is now an element in list d. The question I have now is how can I make a dictionary out of the list d with the car manufacturer as the key and a tuple containing the year and the model should be the key's value. Here is a sample of what list d looks like:

['1899 Horsey Horseless', '1909 Ford Model T', '1911 Overland OctoAuto', '2003 Hummer H2', '2004 Chevy SSR']

Any help would be appreciated!

Use second parameter of split to limit splits to two.

>>> '1958 MGA Twin Cam'.split(None, 2)
['1958', 'MGA', 'Twin Cam']
>>> '1961 Corvair'.split(None, 2)
['1961', 'Corvair']
>>> 

The question I have now is how can I make a dictionary out of the list d with the car manufacturer as the key and a tuple containing the year and the model should be the key's value.

Something like this.

>>> lst = ['1899 Horsey Horseless', '1909 Ford Model T', '1911 Overland OctoAuto', '2003 Hummer H2', '2004 Chevy SSR']
>>> lst = [i.split(' ', 2) for i in lst]
d = {}
>>> for i in lst:
...     d[i[1]] = i[2],i[0]
...     
>>> d
{'Chevy': ('SSR', '2004'),
 'Ford': ('Model T', '1909'),
 'Horsey': ('Horseless', '1899'),
 'Hummer': ('H2', '2003'),
 'Overland': ('OctoAuto', '1911')}

One line.

 >>> dict((v[1],(v[2],v[0])) for v in lst)
{'Chevy': ('SSR', '2004'),
 'Ford': ('Model T', '1909'),
 'Horsey': ('Horseless', '1899'),
 'Hummer': ('H2', '2003'),
 'Overland': ('OctoAuto', '1911')}

If you split as pyTony suggest,you will also get two element in list.
Then you get a IndexError when try to make a dictionary.
The you can do a try,except like this.

>>> d = {}
>>> lst = ['1899 Horsey Horseless', '1909 Ford Model T', '1961 Corvair']
>>> lst = [i.split(' ', 2) for i in lst]
>>> for i in lst:
...     try:
...         d[i[1]] = i[2],i[0]
...     except IndexError:    
...         d[i[1]] = i[0]
...     
>>> d
{'Corvair': '1961',
 'Ford': ('Model T', '1909'),
 'Horsey': ('Horseless', '1899')}

Edited 3 Years Ago by snippsat

This article has been dead for over six months. Start a new discussion instead.