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 of a integer, a, such 0<a<d.), , let k number of distinct rows of a. find k x m array, a', such each row of a appears in a'.


the method: (hashing) hash rows of a table, skipping duplicates , adding rest a'.

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

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -