<div dir="ltr"><div>If I remember correctly, the format of the data looked like this, and the task was to read the restaurant info into dictionaries. (Of course, sane people would write it using a csv variant)</div><div><br>

</div><div>Restaurant A</div><div>87%</div><div>$$$</div><div>Thai</div><div><br></div><div>Restaurant B</div><div>72%</div><div>$$</div><div>Mexican</div><div><br></div><div>Restaurant C</div><div>50%</div><div>$</div><div>

Chinese</div><div><br></div><div>Restaurant D</div><div>90%</div><div>$$$$</div><div>French, Italian</div><div><br></div><div>My solution was:</div><div><br></div><div>f = open("restaurants.txt", "rU")</div>

<div><br></div><div>restaurants = f.read().split("\n\n")</div><div>restaurants = [r.splitlines() for r in restaurants]</div><div>keys = ['name', 'rating', 'price', 'cuisine']</div>

<div>restaurants = [dict(zip(keys, r)) for r in restaurants]</div><div><br></div><div>More functional approach. As Jim and I discussed, this probably wouldn't be a good approach if the list of restaurants in the file was huge. Jim also brought up the case where there might be extraneous newlines between restaurants, which I would probably just solve by changing the splitting on a double newline to using a regex instead.</div>

</div><div class="gmail_extra"><br clear="all"><div>Cheers,<br>Xianyi</div>
<br><br><div class="gmail_quote">On Fri, Apr 12, 2013 at 4:51 PM,  <span dir="ltr"><<a href="mailto:jep200404@columbus.rr.com" target="_blank">jep200404@columbus.rr.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">On Fri, 12 Apr 2013 10:38:24 -0400, Kurtis Mullins <<a href="mailto:kurtis.mullins@gmail.com">kurtis.mullins@gmail.com</a>> wrote:<br>
<br>
> Wouldn't it be more correct to do this?<br>
><br>
> if f.readline():<br>
>     # Do something<br>
> else:<br>
>     break<br>
<br>
</div>That depends on what is in the "...".<br>
In this case, it was desired to _ignore_ a line at the end<br>
of the loop or bail out of the loop when there is no more input.<br>
The "..." had highly naive, deliberately unoptimized code for<br>
some task in a Toronto Python class on coursera. The "..." code<br>
was something vaguely like:<br>
<br>
    name = f.readline().strip()<br>
    rating = f.readline().strip()<br>
    price = f.readline().strip()<br>
    cuisine = f.readline().strip()<br>
    price_to_name[price].append(name)<br>
    cuisine_to_name[cuisine].append(name)<br>
    rating_to_name[cuisine].append(name)<br>
<br>
Of course, that just leads to more questions about the<br>
larger context, to which I refer you to the class.<br>
There was discussion about the wisdom of the input format,<br>
but for the sake of the course, the input format was a fait<br>
accompli, kind of like maintaining other peoples' code in the<br>
real world.<br>
<br>
XY wrote a much shorter, somewhat "functional" approach<br>
using .read(), zip(), dict(), list comprehensions and friends.<br>
There was discussion of the strengths and drawbacks of XY's code<br>
and mine.<br>
<div class="im"><br>
> On Fri, Apr 12, 2013 at 10:16 AM, <<a href="mailto:jep200404@columbus.rr.com">jep200404@columbus.rr.com</a>> wrote:<br>
<br>
</div><div class="im">> Judging by the other information you have in this email, I'm assuming this<br>
> was just an over-view from a meeting you guys had :)<br>
<br>
</div>You're being generous.<br>
<div class="im"><br>
> So my apologies if this is the wrong place for that discussion.<br>
<br>
</div>This is a good place for that discussion. It'd be fun for XY<br>
to show what he remembers of his code, then see who can beat it.<br>
It'd also be fun to see who can write code that is clearer and<br>
more naive than what I wrote.<br>
<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
CentralOH mailing list<br>
<a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/centraloh" target="_blank">http://mail.python.org/mailman/listinfo/centraloh</a><br>
</div></div></blockquote></div><br></div>