diff options
author | danielhrisca <daniel.hrisca@gmail.com> | 2018-12-18 07:53:57 +0200 |
---|---|---|
committer | danielhrisca <daniel.hrisca@gmail.com> | 2018-12-18 07:54:15 +0200 |
commit | e6133b3a3f5382ec0badcfe0b6e069e05ea6e253 (patch) | |
tree | 61d94f1ccd7b519f183778b15e02418a9f802404 /numpy | |
parent | fe9252392d8c3c62062133bffbffab9f72b7de0f (diff) | |
download | numpy-e6133b3a3f5382ec0badcfe0b6e069e05ea6e253.tar.gz |
use OrderedCounter recipe from Python documentation
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/records.py | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py index 7f77bcc83..c777b30a9 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -38,7 +38,7 @@ from __future__ import division, absolute_import, print_function import sys import os import warnings -from collections import OrderedDict +from collections import Counter, OrderedDict from . import numeric as sb from . import numerictypes as nt @@ -74,16 +74,24 @@ _byteorderconv = {'b':'>', numfmt = nt.typeDict +# taken from OrderedDict recipes in the Python documentation +# https://docs.python.org/3.3/library/collections.html#ordereddict-examples-and-recipes +class OrderedCounter(Counter, OrderedDict): + """Counter that remembers the order elements are first encountered""" + + def __repr__(self): + return '%s(%r)' % (self.__class__.__name__, OrderedDict(self)) + + def __reduce__(self): + return self.__class__, (OrderedDict(self),) + def find_duplicate(list): """Find duplication in a list, return a list of duplicated elements""" - counter = OrderedDict() - for item in list: - if item in counter: - counter[item] += 1 - else: - counter[item] = 1 - - return [item for item, counts in counter.items() if counts > 1] + return [ + item + for item, counts in OrderedCounter(list).items() + if counts > 1 + ] @set_module('numpy') |