[CentralOH] 2013-04-11 道場
iynaix
iynaix at gmail.com
Fri Apr 12 23:26:45 CEST 2013
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)
Restaurant A
87%
$$$
Thai
Restaurant B
72%
$$
Mexican
Restaurant C
50%
$
Chinese
Restaurant D
90%
$$$$
French, Italian
My solution was:
f = open("restaurants.txt", "rU")
restaurants = f.read().split("\n\n")
restaurants = [r.splitlines() for r in restaurants]
keys = ['name', 'rating', 'price', 'cuisine']
restaurants = [dict(zip(keys, r)) for r in restaurants]
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.
Cheers,
Xianyi
On Fri, Apr 12, 2013 at 4:51 PM, <jep200404 at columbus.rr.com> wrote:
> On Fri, 12 Apr 2013 10:38:24 -0400, Kurtis Mullins <
> kurtis.mullins at gmail.com> wrote:
>
> > Wouldn't it be more correct to do this?
> >
> > if f.readline():
> > # Do something
> > else:
> > break
>
> That depends on what is in the "...".
> In this case, it was desired to _ignore_ a line at the end
> of the loop or bail out of the loop when there is no more input.
> The "..." had highly naive, deliberately unoptimized code for
> some task in a Toronto Python class on coursera. The "..." code
> was something vaguely like:
>
> name = f.readline().strip()
> rating = f.readline().strip()
> price = f.readline().strip()
> cuisine = f.readline().strip()
> price_to_name[price].append(name)
> cuisine_to_name[cuisine].append(name)
> rating_to_name[cuisine].append(name)
>
> Of course, that just leads to more questions about the
> larger context, to which I refer you to the class.
> There was discussion about the wisdom of the input format,
> but for the sake of the course, the input format was a fait
> accompli, kind of like maintaining other peoples' code in the
> real world.
>
> XY wrote a much shorter, somewhat "functional" approach
> using .read(), zip(), dict(), list comprehensions and friends.
> There was discussion of the strengths and drawbacks of XY's code
> and mine.
>
> > On Fri, Apr 12, 2013 at 10:16 AM, <jep200404 at columbus.rr.com> wrote:
>
> > Judging by the other information you have in this email, I'm assuming
> this
> > was just an over-view from a meeting you guys had :)
>
> You're being generous.
>
> > So my apologies if this is the wrong place for that discussion.
>
> This is a good place for that discussion. It'd be fun for XY
> to show what he remembers of his code, then see who can beat it.
> It'd also be fun to see who can write code that is clearer and
> more naive than what I wrote.
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20130412/f78354b9/attachment-0001.html>
More information about the CentralOH
mailing list