保留订单的numpy.unique

时间:2013-03-26 12:41:00

标签: python numpy

['b','b','b','a','a','c','c']

numpy.unique给出了

['a','b','c']

如何保留原始订单

['b','a','c']

很棒的答案。奖金问题。为什么这些方法都不适用于此数据集? http://www.uploadmb.com/dw.php?id=1364341573以下是问题numpy sort wierd behavior

7 个答案:

答案 0 :(得分:48)

unique()很慢,O(Nlog(N)),但您可以通过以下代码执行此操作:

import numpy as np
a = np.array(['b','a','b','b','d','a','a','c','c'])
_, idx = np.unique(a, return_index=True)
print(a[np.sort(idx)])

输出:

['b' 'a' 'd' 'c']
对于大数组O(N),

Pandas.unique()要快得多:

import pandas as pd

a = np.random.randint(0, 1000, 10000)
%timeit np.unique(a)
%timeit pd.unique(a)

1000 loops, best of 3: 644 us per loop
10000 loops, best of 3: 144 us per loop

答案 1 :(得分:17)

使用return_index的{​​{1}}功能。返回元素首次出现在输入中的索引。然后np.unique那些指数。

argsort

答案 2 :(得分:7)

a = ['b','b','b','a','a','c','c']
[a[i] for i in sorted(np.unique(a, return_index=True)[1])]

答案 3 :(得分:2)

如果您尝试删除已排序的可迭代的重复项,则可以使用itertools.groupby函数:

>>> from itertools import groupby
>>> a = ['b','b','b','a','a','c','c']
>>> [x[0] for x in groupby(a)]
['b', 'a', 'c']

这更像是unix'uniq'命令,因为它假定列表已经排序。当您在未排序的列表上尝试时,您将得到以下内容:

>>> b = ['b','b','b','a','a','c','c','a','a']
>>> [x[0] for x in groupby(b)]
['b', 'a', 'c', 'a']

答案 4 :(得分:1)

如果你想删除重复的条目,比如Unix工具uniq,这是一个解决方案:

def uniq(seq):
  """
  Like Unix tool uniq. Removes repeated entries.
  :param seq: numpy.array
  :return: seq
  """
  diffs = np.ones_like(seq)
  diffs[1:] = seq[1:] - seq[:-1]
  idx = diffs.nonzero()
  return seq[idx]

答案 5 :(得分:1)

#List we need to remove duplicates from while preserving order

x = ['key1', 'key3', 'key3', 'key2'] 

thisdict = dict.fromkeys(x) #dictionary keys are unique and order is preserved

print(list(thisdict)) #convert back to list

output: ['key1', 'key3', 'key2']

答案 6 :(得分:0)

使用OrderedDict(比列表理解要快)

from collections import OrderedDict  
a = ['b','a','b','a','a','c','c']
list(OrderedDict.fromkeys(a))