dictionary 'hash'
anton muhin
antonmuhin at rambler.ru
Fri Dec 19 09:19:59 EST 2003
gyro wrote:
> Hi,
> I have a collection of objects that I am currently trying to manage.
> Each object is initialized with a dictionary. The dictionaries have the
> same keys but may have different values. The initialization process is
> time consuming, so I don't want to create an object if I have already
> instantiated one using a dictionary with the 'same' items.
>
> I would like to collect the objects in a dictionary with the objects as
> values and some identifier or hash of the initializing dictionary as the
> keys.
>
>
> For instance:
>
>
> idicts = [{'k1':val1,'k2':val2},
> {'k1':val3,'k2':val4},
> {'k1':val1,'k2':val2}]
>
> for idict in idicts:
> dhash = somehashingfunction(idict)
> if not objectstoragedict.has_key(dhash):
> newobj = MyClass(idict)
> objectstoragedict[dhash] = newobj
>
>
> Any ideas for 'somehashingfunction'?
> The values in idict may be strings, integers, or instances of other
> classes I have created.
>
> Presently, I am considering something like
>
> def somehashingfunction(rdict):
> import sha
> dkeys = rdict.keys()
> dkeys.sort()
> rvals = [repr(rdict[key]) for key in dkeys]
> rstr = '_'.join(rvals)
> dhash = sha.new(rstr).hexdigest()
> return dhash
>
>
> I'd appreciate any suggestions or comments.
>
> Thanks.
>
> -g
>
Actually, it seems that the problem might be bypassed by other design,
but you know it better. If you really need it, you approach seems
resonable. However, sometimes you can store data as a tuple or list and
map keys to inidices---in this case you can use standard comparision
operators. A word of caution: if store mutable objects, there might be
some problems.
hth,
anton.
More information about the Python-list
mailing list