python one-liner

python one-linerの使い方

タグ one-liner


概要

  • shellのコマンドラインの中で複雑なことをやりたいときに便利
  • perlのように本当に一行で書くのは難しく、ヒアドキュメントのような方法で書くことになる

具体例(word countの例)

$ echo "the day is sunny the the
the sunny is is
" | python3 -c '
import sys
from collections import Counter
a = [x.split() for x in sys.stdin.read().split("\n")]
t = []
for x in a:
    for y in x:
        t.append(y)
for x, y in sorted(Counter(t).items(), key=lambda x:-x[1]):
    print(x, y)
'

参考