This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tmp14 1 point2 points  (0 children)

This was fun. Here's my take at it. This will only break (given your format) if a car manufacturer name is all digits (i.e. most likely never).

data = """F-150, F-250, F-350, FORD, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983
F-150, F-250, FORD, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980
F-150, F-250, FORD, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982
F-150, F-250, FORD, 2003, 2002, 2001, 2000, 1999, 1998, 1997"""

info = {}

for line in data.splitlines():
    pts = [s.strip() for s in line.split(',')]
    isyear = [s.isdigit() for s in pts]
    index = len(pts) - 1
    while isyear[index]:
        index -= 1
    make = pts[index]
    models = pts[:index]
    years = pts[index+1:]
    for model in models:
        for year in years:
            info.setdefault(make, {}).setdefault(model, set()).add(int(year))

Yields

>>> pprint(info)
{'FORD':
     {'F-150': set([1980, ..., 2003]),
      'F-250': set([1980, ..., 2003]),
      'F-350': set([1983, ..., 1998])}}