Good Python style?
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Thu May 31 06:19:28 EDT 2007
On Thu, 31 May 2007 09:59:07 +0200, 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])])
It's a little complex, but not excessively so. Any more, and it would
probably be too complex.
It would probably be easier to read with more readable names and a few
comments:
some_set = frozenset(
# ... from a list comp of the first word in each line
[line.split()[0] for line in
# ... each line has leading and trailing white space
# filtered out, and blanks are skipped
filter(None, # strip whitespace and filter out blank lines
[line.strip() for line in input_lines]
)])
Splitting it into multiple lines is self-documenting:
blankless_lines = filter(None, [line.strip() for line in input_lines])
first_words = [line.split()[0] for line in blankless_words]
some_set = frozenset(first_words)
As for which is faster, I doubt that there will be much difference, but I
expect the first version will be a smidgen fastest. However, I'm too lazy
to time it myself, so I'll just point you at the timeit module.
--
Steven.
More information about the Python-list
mailing list