Re: Good Python style?
- From: Michael Bentley <michael@xxxxxxxxxxxxxxxxx>
- Date: Thu, 31 May 2007 03:53:40 -0500
On May 31, 2007, at 2:59 AM, Andreas Beyer wrote:
Hi,
I found the following quite cryptic code, which basically reads the
first column of some_file into a set.
In Python I am used to seeing much more verbose/explicit code. However,
the example below _may_ actually be faster than the usual "for line in ..."
Do you consider this code good Python style? Or would you recommend to
refrain from such complex single-line code??
Thanks!
Andreas
inp = resource(some_file)
# read first entries of all non-empty lines into a set
some_set = frozenset([line.split()[0] for line in \
filter(None, [ln.strip() for ln in inp])])
I don't know about style, but I find it much harder to read than this:
some_set = frozenset(line.split()[0]
for line in inp
if len(line.strip()))
....which I *think* is equivalent, but less complicated.
regards,
Michael
---
(do (or (do (not '(there is no try))) ()))
.
- Prev by Date: Re: Anyone else has seen "forrtl: error (200) ..."
- Next by Date: Re: file / module / package - import problem
- Previous by thread: Re: Good Python style?
- Next by thread: Researcher/Lecturer Position at MODUL University Vienna
- Index(es):
Relevant Pages
|