Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'element': 0.07; 'indexing': 0.07; 'subject:file': 0.07; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.11; "'0',": 0.16; 'appreciated!': 0.16; 'cc:name:python list': 0.16; 'separated': 0.16; 'subject:.txt': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'two.': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**1': 0.23; 'header:User-Agent:1': 0.23; '31,': 0.24; 'subject: .': 0.24; 'looks': 0.24; 'question': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; "skip:' 10": 0.31; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'manufacturer': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'thank': 0.38; 'sure': 0.39; 'how': 0.40; 'march': 0.61; 'here': 0.66; 'sample': 0.67; 'iron': 0.68; 'containing': 0.69; 'car': 0.72; 'ordered.': 0.84; 'washington': 0.93; '2013': 0.98 X-Received: by 10.49.120.67 with SMTP id la3mr588007qeb.12.1364748770738; Sun, 31 Mar 2013 09:52:50 -0700 (PDT) Newsgroups: comp.lang.python Date: Sun, 31 Mar 2013 09:52:50 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=99.9.126.19; posting-account=bb1tZgkAAABmwkKlzWV6XDrCLEIC9th2 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 99.9.126.19 MIME-Version: 1.0 Subject: Re: Creating a dictionary from a .txt file From: "C.T." To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: "C.T." , Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364749257 news.xs4all.nl 6984 [2001:888:2000:d::a6]:38990 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42382 On Sunday, March 31, 2013 12:20:25 PM UTC-4, zipher wrote: > Every line is now an element in list d. The question I have now is how ca= n I make a dictionary out of the list d with the car manufacturer as the ke= y 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: >=20 >=20 >=20 >=20 > ['1899 Horsey Horseless', '1909 Ford Model T', '1911 Overland OctoAuto', = '2003 Hummer H2', '2004 Chevy SSR'] >=20 >=20 >=20 > Any help would be appreciated! >=20 >=20 >=20 >=20 > As long as your data is consistently ordered, just use list indexing. =A0= d[2] is your key, and (d[1],d[3]) the key's value. >=20 >=20 >=20 > Mark > Tacoma, Washington Thank you, Mark! My problem is the data isn't consistently ordered. I can u= se slicing and indexing to put the year into a tuple, but because a car man= ufacturer could have two names (ie, Aston Martin) or a car model could have= two names(ie, Iron Duke), its harder to use slicing and indexing for those= two. I've added the following, but the output is still not what I need it= to be. t=3D{} for i in d : t[d[d.index(i)][5:]]=3D tuple(d[d.index(i)][:4]) print (t) The output looks something like this: {'Ford Model T': ('1', '9', '0', '9'), 'Mosler Consulier GTP': ('1', '9', '= 8', '5'), 'Scripps-Booth Bi-Autogo': ('1', '9', '1', '3'), 'Morgan Plus 8 P= ropane': ('1', '9', '7', '5'), 'Fiat Multipla': ('1', '9', '9', '8'), 'Ford= Pinto': ('1', '9', '7', '1'), 'Triumph Stag': ('1', '9', '7', '0'), 'BMW 7= -series': ('2', '0', '0', '2')} Here the key is the car manufacturer and car model and the value is a tuple= containing the year separated by a comma.( Not sure why that is ?)