python 2.7 - Use of a dictionary + Bitarray -
i need know if bitarray in dictionary not why code not work, idea?
dic={} bb=bitarray() bb.append(false) temp=bitarray() temp.append(false) dic[bb]="" if temp in dic: print("hello")
this tricky one. dict's keys should hashable. said, let's take @ hashes generated both bitarrays.
>>> bb bitarray('0') >>> temp bitarray('0') >>> hash(bb) 1871851 >>> hash(temp) 1871843
therefore, when attempt if temp in dic:
, hash of temp not found in dic. therefore don't desired result. don't know why hash of bb , temp different.
you instead:
>>> dic[bb.tostring()] = 'test' >>> if temp.tostring() in dic: ... print 'hello' ... hello
you use bb.tobytes()
dict's key if desire. in case, use temp.tobytes()
comparison.
Comments
Post a Comment