Python single line (convert perl to pyp)

I was wondering if it is possible to do single-line with pyp , which has the same functions as this one.

perl -l -a -F',' -p -e'if ($. > 1) { $F[6] %= 12; $F[7] %= 12;$_ = join(q{,}, @F[6,7]) }' 

This takes a comma-separated list of numbers with 8 numbers in a line and displays it in the same format, except that the last two numbers in each line are abbreviated modulo 12. It also displays the first line (header line) verbatim.

I have quite a few of these obscure perl single-line, and in the first case I would like to translate them all into python.

+6
source share
1 answer

For the record, I'm not sure I approve. Writing the code horizontally does not seem to me much better than writing it vertically, and - in a friendly way - I am a little skeptical about this, in practice it may seem in practice. One of the joys of Python is that you no longer have to worry about writing GolfScript.

However, how about:

 pyp "mm | p if n==0 else (p[:-2] + [(int(x)%12) for x in p[-2:]]) | mm" 

which produces:

 localhost-2:coding $ cat exam.pyp a,b,c,d,e,f,g,h 11,22,33,44,55,66,77,88 12,23,34,45,56,67,78,89 13,24,35,46,57,68,79,80 localhost-2:coding $ cat exam.pyp | pyp "mm | p if n==0 else (p[:-2] + [(int(x)%12) for x in p[-2:]]) | mm" a,b,c,d,e,f,g,h 11,22,33,44,55,66,5,4 12,23,34,45,56,67,6,5 13,24,35,46,57,68,7,8 

[Disclaimer: This is my first ever pyp program that I downloaded about ten minutes ago.]

+4
source

Source: https://habr.com/ru/post/944290/


All Articles