私が抱えているこの問題を誰かがすぐに解決してくれることを願っています。iterable 内のユーザー定義オブジェクトの出現回数をカウントできるようにしたいと考えています。問題は、オブジェクトを比較するオブジェクトを作成すると、メモリ空間に別のオブジェクトが作成されるため、オブジェクトがカウントされるべきときにカウントされないことです。
例:
class Barn:
def __init__(self, i,j):
self.i = i
self.j = j
barns = [Barn(1,2), Barn(3,4)]
a = Barn(1,2)
print 'number of Barn(1,2) is', barns.count(Barn(1,2))
print 'memory location of Barn(1,2) in list', barns[0]
print 'memory location of Barn(1,2) stored in "a"', a
戻り値:
number of Barn(1,2) is 0
memory location of Barn(1,2) in list <__main__.Barn instance at 0x01FCDFA8>
memory location of Barn(1,2) stored in "a" <__main__.Barn instance at 0x01FD0030>
count
リスト内の各項目に名前を付けずに、リストのメソッドをこのインスタンスで機能させる方法はありますか?