[Tutor] Dictionaries and multiple keys/values
Hi again, Tutor List. I am trying to figure out a problem I've run into. Let me first say that this is an assignment, so please don't give me any answers, but just nudge me in the general direction. So the task is this: from a text file, populate three different dictionaries with various information. The text file is structured like so: Georgie Porgie 87% $$$ Canadian, Pub Food So name, rating, price range, and food offered. After food offered follows a blank line before the next restaurant is listed. The three dictionaries are: name_to_rating = {} price_to_names = {'$': [], '$$': [], '$$$': [], '': []} cuisine_to_names = {} Now I've poked at this for a while now, and one idea I had, which I worked on for quite a while, was that since the restaurants all start at index 0, 5, 10 and so on, I could structure a while loop like this: with open('textfile.txt') as mdf: file_length = len(mdf.readlines())-1 mdf.seek(0) data = mdf.readlines() i = 0 while file_length > 0: name_to_rating[data[i]] = int(data[i+1][:2]) price_to_names[data[i+2].strip()].append(data[i].strip()) # here's the cuisine_to_names part i += 5 file_length -= 5 And while this works, for the two first dictionaries, it seems really cumbersome -- especially that second expression -- and very, very brittle. However, even if I was happy with that, I can't figure out what to do in the situation where: data[i+3] = 'Canadian, Pub Food' #should be two items, is currently a string. My problem is that I'm... stupid. I can split the entry into a list with two items, but even so I don't know how to add the key: value pair to the dictionary so that the value is a list, which I then later can append things to. I'm sorry, this sounds terribly confused, I know. I had another idea to feed each line to a function, because no restaurant name has a comma in it, and food offered always has a comma in it if the restaurant offers more than one kind. But again, this seems really brittle. I guess we can't use objects (for some reason), but that doesn't really matter because if I can't extract the data into dictionaries I wouldn't have much use of an object either way. So yeah, my two questions are these: is there a better way to move through the text file other than a really convoluted expression? And how do I add more than one value to a key in a dictionary, if the values are added at different times and there's no list created in the dictionary to begin with? (I briefly though about initializing empty lists for each food type in the dictionary and go with my horrible expressions, but that seems like a cheap way out of a problem I'd rather tackle in a good way to begin with) Much thanks in advance. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionaries and multiple keys/values
Hi, On Tue, Mar 26, 2013 at 5:36 AM, Robert Sjoblom wrote: > Hi again, Tutor List. > > I am trying to figure out a problem I've run into. Let me first say > that this is an assignment, so please don't give me any answers, but > just nudge me in the general direction. So the task is this: from a > text file, populate three different dictionaries with various > information. The text file is structured like so: > Georgie Porgie > 87% > $$$ > Canadian, Pub Food > > So name, rating, price range, and food offered. After food offered > follows a blank line before the next restaurant is listed. > > The three dictionaries are: > name_to_rating = {} > price_to_names = {'$': [], '$$': [], '$$$': [], '': []} > cuisine_to_names = {} > > Now I've poked at this for a while now, and one idea I had, which I > worked on for quite a while, was that since the restaurants all start > at index 0, 5, 10 and so on, I could structure a while loop like this: > with open('textfile.txt') as mdf: > file_length = len(mdf.readlines())-1 > mdf.seek(0) > data = mdf.readlines() > No need to calculate the file length. Read yoiur data & ask python for the length of data. You don't need nor want to read the file twice. > i = 0 > while file_length > 0: > name_to_rating[data[i]] = int(data[i+1][:2]) > price_to_names[data[i+2].strip()].append(data[i].strip()) > # here's the cuisine_to_names part > i += 5 > file_length -= 5 > Another approach could be to process the file while reading it. Since the format is fixed, you knoow exactly what information the line contains. > > And while this works, for the two first dictionaries, it seems really > cumbersome -- especially that second expression -- and very, very > brittle. However, even if I was happy with that, I can't figure out > what to do in the situation where: > data[i+3] = 'Canadian, Pub Food' #should be two items, is currently a > string. > My problem is that I'm... stupid. I can split the entry into a list > with two items, but even so I don't know how to add the key: value > pair to the dictionary so that the value is a list, which I then later > can append things to. > You might want to read up on the dict.get() method. > > I'm sorry, this sounds terribly confused, I know. I had another idea > to feed each line to a function, because no restaurant name has a > comma in it, and food offered always has a comma in it if the > restaurant offers more than one kind. But again, this seems really > brittle. > > Much thanks in advance. > -- > best regards, > Robert S. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor