Python Golf

Jon Ribbens jon+usenet at unequivocal.eu
Tue Nov 7 11:06:07 EST 2023


On 2023-11-07, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>   I read this in a shell newsgroup:
>
> perl -anE '$s += $F[1]; END {say $s}' in
>
>   , so I wrote
>
> py -c "import sys; print(sum(int(F.split()[1])for F in sys.stdin))" <in
>
>   to show that this is possible with Python too. 
>
>   But now people complain that it's longer than the Perl version.
>
>   Do you see ways to make it shorter (beyond removing one space
>   after the semicolon ";")?

It's a bit of an unfair competition given that, unlike Perl,
Python is not designed to be an 'awk' replacement.

Having said that, you could make it a bit shorter:

py -c "print(sum(int(F.split()[1])for F in open(0)))" <in


More information about the Python-list mailing list