Python一线(将perl转换为pyp) [英] Python one-liner (converting perl to pyp)

查看:111
本文介绍了Python一线(将perl转换为pyp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能使用具有相同功能的 pyp 制作单线纸.

I was wondering if its possible to make a one-liner with pyp that has the same functionality as this.

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

这将接受一个逗号分隔的数字列表,每行有8个数字,并以相同的格式输出,除了每行的最后两个数字以模12进行模化.它还逐字输出第一行(标题行)

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

我有很多这类晦涩的perl线性代码,我想首先将它们全部翻译成python.

I have quite a lot of these obscure perl one-liners and I would like in the first instance to translate them all to python.

推荐答案

出于记录,我不确定我是否同意.在我看来,水平编写代码并不比垂直编写更好,而且-以一种友好的方式-我有点怀疑,这样做在实践中提供了许多优点. Python的乐趣之一就是您不必再担心编写GolfScript.

For the record, I'm not sure I approve. Writing code horizontally doesn't seem to me that much better than writing it vertically, and -- in a friendly way -- I'm a little sceptical this offers as many advantages in practice as it might seem. One of the joys of Python is that you no longer have to worry about writing GolfScript.

那话,怎么样:

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

产生:

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

[免责声明:这是我有史以来第一个pyp程序,大约十分钟前我下载了它.]

[Disclaimer: this is my first-ever pyp program, which I downloaded about ten minutes ago.]

这篇关于Python一线(将perl转换为pyp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆