オブジェクト (Foo) のリストがあります。Foo オブジェクトにはいくつかの属性があります。Foo オブジェクトのインスタンスは、すべての属性が等しい場合 (その場合にのみ)、Foo オブジェクトの別のインスタンスと同等 (等しい) です。
次のコードがあります。
class Foo(object):
def __init__(self, myid):
self.myid=myid
def __eq__(self, other):
if isinstance(other, self.__class__):
print 'DEBUG: self:',self.__dict__
print 'DEBUG: other:',other.__dict__
return self.__dict__ == other.__dict__
else:
print 'DEBUG: ATTEMPT TO COMPARE DIFFERENT CLASSES:',self.__class__,'compared to:', other.__class__
return False
import copy
f1 = Foo(1)
f2 = Foo(2)
f3 = Foo(3)
f4 = Foo(4)
f5 = copy.deepcopy(f3) # overkill here (I know), but needed for my real code
f_list = [f1,f2,f3,f4,f5]
# Surely, there must be a better way? (this dosen't work BTW!)
new_foo_list = list(set(f_list))
単純な型 (int、float、string、そして驚くべきことに datetime.datetime 型) を扱う場合、上記の (set と back に変換する) この小さな (アンチ?) 'パターン' をよく使用しましたが、より多くの関連するデータ型 - 上記の Foo のように。
では、上記のリスト f1 を一意のアイテムのリストに変更するにはどうすればよいでしょうか。各アイテムをループして一時キャッシュなどに既に存在するかどうかを確認する必要はありません。
これを行うための最もpythonicな方法は何ですか?