How to hash a row in Python? -
i trying implement following algorithm in python [1]:
the problem: (row compression) let
a
n x m
array of bounded degree d (i.e., each element ofa
integer,a
, such0<a<d
.), , letk
number of distinct rows ofa
. findk x m
array,a'
, such each row ofa
appears ina'
.
the method: (hashing) hash rows of
a
table, skipping duplicates , adding resta'
.
i not understand how hash row in python?
well, understand mean "hash rows of table". understand following. suppose have matrix this:
a = [[1, 2, 3, 4], [1, 2, 3, 4], [6, 7, 5, 4]]
so, hash rows (somehow) , get:
b = [x1, x2, x3]
where xi
hash of row i
. here have x1=x2
since row 1 , row 2 equal. since x1=x2
, keep 1 , have:
a' = [[1, 2, 3, 4], [6, 7, 5, 4]]
am right? if so, how can implement such algorithm in python?
thanks.
[1] d. comer, "removing duplicate rows of bounded degree array using tries", purdue university, 1977 .
first of all, need remove duplicated rows. aim, can use set
first need convert rows immutable object types.
you can convert them tuple
:
>>> = [[1, 2, 3, 4], ... [1, 2, 3, 4], ... [6, 7, 5, 4]] >>> >>> map(tuple,a) [(1, 2, 3, 4), (1, 2, 3, 4), (6, 7, 5, 4)]
then can use set
. , set
uses hash function, result hashed automatically :
>>> set(map(tuple,a)) set([(1, 2, 3, 4), (6, 7, 5, 4)])
Comments
Post a Comment