我想刪除冗餘元組,但保留外觀順序。我看了類似的問題。這個問題Find unique rows in numpy.array看起來很有希望,但不知何故,它不適合我。如何從元組列表中刪除重複項,但保留原始順序
我可以在這個答案中使用熊貓(https://stackoverflow.com/a/14089586/566035),但我不喜歡使用熊貓,因此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')
哇。這也很好。謝謝! – otterb 2014-09-03 18:21:54
很難挑選答案,但我認爲我更喜歡這個。感謝大家! – otterb 2014-09-03 18:57:30
哇 - 這種排序和索引操作的開銷是驚人的...我不會說它「效率不高」 - 我會說它真的很差:( – 2014-09-03 23:05:03