如何从元组列表中删除重复项但保留原始顺序

时间:2014-09-03 17:31:51

标签: python sorting numpy unique

我想删除多余的元组但保留外观的顺序。我看了类似的问题。这个问题Find unique rows in numpy.array看起来非常有希望但不知何故它对我不起作用。

我可以在这个答案中使用pandas(https://stackoverflow.com/a/14089586/566035),但我不喜欢使用pandas,这样py2exe生成的可执行文件就会很小。

import numpy as np

data = [('a','z'), ('a','z'), ('a','z'), ('1','z'), ('e','z'), ('c','z')]

#What I want is:
    array([['a', 'z'],
           ['1', 'z'],
           ['e', 'z'],
           ['c', 'z']], 
          dtype='|S1')

#What I have tried:
# (1) numpy.unique, order not preserved
np.unique(data)

    array([['a', 'z'],
           ['c', 'z'],
           ['1', 'z'],
           ['e', 'z']], 
          dtype='|S1')

# (2) python set, order not preserved
set(data)

    set([('1', 'z'), ('a', 'z'), ('c', 'z'), ('e', 'z')])

# (3) answer here : https://stackoverflow.com/a/16973510/566035, order not preserved
a = np.array(data)
b = np.ascontiguousarray(a).view(np.dtype((np.void, a.dtype.itemsize * a.shape[1])))
_, idx = np.unique(b, return_index=True)

a[idx]

    array([['1', 'z'],
           ['a', 'z'],
           ['c', 'z'],
           ['e', 'z']], 
          dtype='|S1')

2 个答案:

答案 0 :(得分:2)

这在效率方面不是很好,但是可读代码非常简单,可以用于较小的列表:

sorted(set(data), key=data.index)

答案 1 :(得分:1)

糟糕!我自己找到了答案......

seen = set()
np.array([x for x in data if x not in seen and not seen.add(x)])

# output
array([['a', 'z'],
       ['1', 'z'],
       ['e', 'z'],
       ['c', 'z']], 
      dtype='|S1')