Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A dictionary with a unique possible value for each key?

I need to create a map from twitter status IDs to their author ID. Obviously, each status has exactly one author.

I expected Python collections to have something like uniqdict class for which d[key] = value will raise an exception if the key already has a value different from value:

class uniqdict(dict):
    def __setitem__(self,key,value):
        try:
            old = super(uniqdict,self).__getitem__(key)
            if old != value:
                raise ValueError(self.__class__.__name__,key,old,value)
        except KeyError:
            super(uniqdict,self).__setitem__(key,value)

Q: Is there a standard name for this kind of dictionary/map/hash table?

like image 909
sds Avatar asked Jun 07 '17 18:06

sds


1 Answers

This looks like a duplicate of Write-once dictionary?

Anyway, I think write-once dictionary is the name you're looking for

like image 94
Shmulik Asafi Avatar answered Oct 18 '22 20:10

Shmulik Asafi