perl转python,Python一线(将perl转换为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]) }'

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.

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.

解决方案

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.

That said, 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, which I downloaded about ten minutes ago.]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值