Skip to content

Instantly share code, notes, and snippets.

@alpham
Created May 23, 2016 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpham/c02ee51f6006236a415f56b54103debc to your computer and use it in GitHub Desktop.
Save alpham/c02ee51f6006236a415f56b54103debc to your computer and use it in GitHub Desktop.
from itertools import groupby
def pack(arr):
uniqe_items = []
count_items = []
for i in groupby(arr):
if i[0] not in uniqe_items:
count_items.append(len(list(i[1])))
uniqe_items.append(i[0])
return ["{}:{}".format(num, count) for num, count in zip(uniqe_items, count_items)]
print pack([5, 5, 5, 7, 7, 3, 4, 7])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment