Skip to content

Instantly share code, notes, and snippets.

@puhitaku
Last active August 29, 2015 14:20
Show Gist options
  • Save puhitaku/cda43b99abf1e82be3e5 to your computer and use it in GitHub Desktop.
Save puhitaku/cda43b99abf1e82be3e5 to your computer and use it in GitHub Desktop.
One-liners in Python
#文字数を各文字ごとに数えてタプルのリストで返す
#Count characters and return the result with list of tuples.
#e.x. c('bbaaac') = [('a', 3), ('b', 2), ('c', 1)]
count = lambda s: [(c,s.lower().count(c)) for c in sorted(set(s.lower()))]
#6ケタのHEXを3つのDECのリストにして返す
#Convert 6 digits HEX in 3 decimals and return tuple.
#e.x. d('0099CC') = [0, 153, 204]
hexcol = lambda h: [int(h[i:i+2],16) for i in range(0, len(h), 2)]
#RLE (Run Length Encoding)
from functools import reduce
from itertools import groupby()
from operator import add
rle = lambda s: reduce(add, [str(len(list(g)))+k for k,g in groupby(s)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment