On 26/03/13 04:36, Robert Sjoblom wrote:

Georgie Porgie
87%
$$$
Canadian, Pub Food


So a 5 line pattern with 4 data fields. The last one containing multiple comma separated values, potentially.


The three dictionaries are:
name_to_rating = {}
price_to_names = {'$': [], '$$': [], '$$$': [], '$$$$': []}
cuisine_to_names = {}

And the keys are all basic single data values


So far so good.

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:

Sounds way too complicated.

I'd just read the file in groups of 5 lines and process them as I went.
I'd also write a helper function to process each record.

In pseudo code

def storeRestaurant(mdr):
       name = mdr.readline()
       rating = mdr.readline()
       price = mdr.readline()
       cuisines= mdr.readline().split(',')
       # now assign values to dictionaries.

with open....as mdr
   while True:
      try: storeRestaurant(mdr)
      except end of file: pass


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'm not sure why you think its brittle?
You need to code in error checking somewhere,
it might as well be hidden in a function.

I guess we can't use objects (for some reason),

Technically you can but maybe assignment wise you can't...

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?

You can create a list to begin with if you suspect you may need multiple values.... ie each time you add a new key create a list.
The get() method should help here.

(I briefly though about initializing empty lists for each food type in
the dictionary and go with my horrible expressions,

You can do the empty lists without the horrible expressions!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to