Jibrilat
Jibrilat

Reputation: 347

Is it possible to generate the same id from identical lists but with different order?

I have 2 lists (unique elements) (mainly lists of strings).

l1 = ['a','b','c']
l2 = ['c','b','a']

and I would like to have a function:

def foo_id(list)

where it generates an id based on their elements but without taking into account the order of the elements in my list.

i.e

foo_id(l1) == foo_id(l2)

Upvotes: 0

Views: 65

Answers (2)

Massifox
Massifox

Reputation: 4487

Try this code:

from hashlib import blake2b

def foo_id(l):
    h = blake2b()
    h.update(str(sorted(l)).encode('ascii'))
    return h.hexdigest()

l1 = ['a','b','c']
l2 = ['c','b','a']
foo_id(l1) == foo_id(l2)
# output: True

Note:
It is not possible to directly use the Python hash() since in executions it would give different results: this is because from Python3 the hash() function is associated with a session seed(PYTHONHASHSEED) for generating random numbers. Find more information in this post.

Upvotes: 1

DarrylG
DarrylG

Reputation: 17166

You could generate unique ids as follows.

import hashlib # provides many hash functions including md5, sha1, sha2, etc.

l1 = ['a','b','c']
l2 = ['c','b','a']

def genereate_id(l):
  s = str(sorted(l))    
  s_unicode = s.encode('utf-8') # hashlib requires unicode
  return hashlib.md5(s_unicode).hexdigest()

print(genereate_id(l1)) # eea457285a61f212e4bbaaf890263ab4
print(genereate_id(l2)) # eea457285a61f212e4bbaaf890263ab4

Upvotes: 1

Related Questions