Python: finding keys with unique values in a dictionary?

前端 未结 9 2071
臣服心动
臣服心动 2021-02-19 01:28

I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.

I will clarify with an exa

9条回答
  •  猫巷女王i
    2021-02-19 01:38

    A little more verbose, but does need only one pass over a:

    revDict = {}
    for k, v in a.iteritems():
      if v in revDict:
         revDict[v] = None
      else:
         revDict[v] = k
    
    [ x for x in revDict.itervalues() if x != None ]
    

    ( I hope it works, since I can't test it here )

提交回复
热议问题