pythonでuniq

id:gi-chiさんのエントリを読んで
Rubyにはuniqがあるんだと知りました。

配列操作の比較表 Ruby, Java5, Python - 制作日誌ギーチ!

自分用にRuby, Java5, Pythonの配列操作比較表を作成しました。

それpy!ということで、これが速いでしょうか。


def uniq(seq):
seen = set()
return [x for x in seq if x not in seen and not seen.add(x)]

こちらのサイトに検証記事とテストプログラムがあります。

Fastest way to uniqify a list in Python - Peterbe.com

From the comments I've now added a couple of more functions to the benchmark. Some which don't support uniqify a list of objects that can't be hashed unless passed with a special hashing method. So see all the functions download the file

Here are the new results:

 * f5 10.1
 * f5b 9.99
 * f8 6.49
 * f10 6.57
 * f11 6.6
 f1 4.28
 f3 3.55
 f6 4.03
 f7 2.59
 f9 2.58

(f2 and f4) were too slow for this testdata.


関連:
重複なしリスト - 鯨飲馬食コード
pythonでuniq - pyletの日記
重複なしリストの作り方(いわゆるuniq) - 図書館断想