Re: hashing an array - howto

2008-09-11 Thread sudhi . herle
On Sep 5, 9:38 am, Helmut Jarausch <[EMAIL PROTECTED]> wrote: > Hi, > > I need to hash arrays of integers (from the hash module). > > So, I have to derive from array and supply a __hash__ method. I don't believe you need to derive from an array. Here are two simple and well known hash functions y

Re: hashing an array - howto

2008-09-08 Thread Robert Kern
[EMAIL PROTECTED] wrote: John Machin: Consider this:>>> hash(123) == hash(123.0) == hash(123L) True Right... Can you explain me why Python designers have chosen to build a hash() like that? Because that's the kind of hash that dicts expect. If two objects are equal (i.e. (x==y) is True), th

Re: hashing an array - howto

2008-09-05 Thread John Machin
On Sep 6, 9:30 am, John Machin <[EMAIL PROTECTED]> wrote: > On Sep 6, 7:49 am, [EMAIL PROTECTED] wrote: > > > John Machin: > > > > Consider this:>>> hash(123) == hash(123.0) == hash(123L) > > > True > > > Right... Can you explain me why Python designers have chosen to build > > a hash() like that?

Re: hashing an array - howto

2008-09-05 Thread John Machin
On Sep 6, 7:49 am, [EMAIL PROTECTED] wrote: > John Machin: > > > Consider this:>>> hash(123) == hash(123.0) == hash(123L) > > True > > Right... Can you explain me why Python designers have chosen to build > a hash() like that? I can't channel them; my rationalisation is this: Following the Law of

Re: hashing an array - howto

2008-09-05 Thread bearophileHUGS
John Machin: > Consider this:>>> hash(123) == hash(123.0) == hash(123L) > True Right... Can you explain me why Python designers have chosen to build a hash() like that? > Try "uses all the information that is relevant to the task". My knowledge of hash data structures seems not enough to unders

Re: hashing an array - howto

2008-09-05 Thread John Machin
On Sep 6, 2:45 am, [EMAIL PROTECTED] wrote: > Michael Palmer: > > > why can't it be tuple already? > > Because if the input list L has tuples and lists, they end having the > same hash value: Perhaps the OP shouldn't be constructing the hash of a mutable object. Perhaps he would be grateful if his

Re: hashing an array - howto

2008-09-05 Thread bearophileHUGS
Michael Palmer: > why can't it be tuple already? Because if the input list L has tuples and lists, they end having the same hash value: >>> L = [[1,2,3], (1,2,3)] >>> hash(tuple(L[0])), hash(tuple(L[1])) (-378539185, -378539185) But it's a not much common situation, and few hash collision pairs

Re: hashing an array - howto

2008-09-05 Thread Michael Palmer
On Sep 5, 11:18 am, [EMAIL PROTECTED] wrote: > Helmut Jarausch: > > > I need to hash arrays of integers (from the hash module). > > One of the possible solutions is to hash the equivalent tuple, but it > requires some memory (your sequence must not be tuples already): why can't it be tuple already

Re: hashing an array - howto

2008-09-05 Thread bearophileHUGS
Helmut Jarausch: > I need to hash arrays of integers (from the hash module). One of the possible solutions is to hash the equivalent tuple, but it requires some memory (your sequence must not be tuples already): assert not isinstance(somelist, tuple) hash(tuple(somelist)) This is an alternative

Re: hashing an array - howto

2008-09-05 Thread Peter Otten
Helmut Jarausch wrote: > I need to hash arrays of integers (from the hash module). > > So, I have to derive from array and supply a __hash__ method. > But how to hash an array (of fixed length, say 25)? > What I need is a function which maps a tuple of 25 integers > into 1 integer with good hashi

hashing an array - howto

2008-09-05 Thread Helmut Jarausch
Hi, I need to hash arrays of integers (from the hash module). So, I have to derive from array and supply a __hash__ method. But how to hash an array (of fixed length, say 25)? What I need is a function which maps a tuple of 25 integers into 1 integer with good hashing properties. Does anybody k