Last active
August 29, 2015 14:06
-
-
Save elleryq/46433fa897c51bc39892 to your computer and use it in GitHub Desktop.
將連續元素分組,然後找出出現次數最多的。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from itertools import groupby | |
| from collections import Counter | |
| from pprint import pprint | |
| def getContinuousItemsCount(l): | |
| x = [tuple(g) for k, g in groupby(l)] | |
| tupleCounter = Counter(x) | |
| s = set(x) | |
| d = {} | |
| for t in s: | |
| d[t] = tupleCounter[t] | |
| return d | |
| pprint(getContinuousItemsCount( | |
| ['A', 'B', 'A', 'A', 'B', 'A', 'A', 'A', 'B', 'A'])) | |
| pprint(getContinuousItemsCount( | |
| ['H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H'])) | |
| pprint(getContinuousItemsCount( | |
| ['H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'H'])) | |
| pprint(getContinuousItemsCount( | |
| ['H', 'T', 'T', 'T', 'H', 'H', 'H', 'H', 'T', 'T'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment