How to get list of keys that share a value with another key within the same dictionary?

限于喜欢 提交于 2019-12-14 03:35:15

问题


I have a dictionary of unique keys where some keys share the same value.

For example:

D = {'ida':{'key':'1'},'idb':{'key':'2'},'idc':{'key':'3'},'idd':{'key':'3'},'ide':{'key':'4'},'idf':{'key':'4'},'idg':{'key':'4'}}

I want a list of keys that share the same value with other keys.

In this case, it would be

l = ['idc','idd','ide','idf','idg']

However, I want to exclude one key from all sets of keys that share the same value.

For example, I'd like to have the keys

l = ['idd','idf','idg']

which excludes 'idc' and 'ide'

or it could be

l = ['idc','ide','idf']

which excludes 'idd' and 'idg'.


回答1:


If the value appears more than once the list comp will add the key.

dup_keys = [k for k in D if sum(D[k] in x for x in D.iteritems()) > 1 ]

['idf', 'idg', 'idd', 'ide', 'idc']

dup_keys[1:]
['idg', 'idd', 'ide', 'idc']
dup_keys[1:-1]
['idg', 'idd', 'ide']

if sum(D[k] in x for x in D.iteritems()) > 1 checks the value appears > 1 times.

To ignore certain keys add some and condition, I am not sure on what basis you want to ignore the keys.




回答2:


# aDictionary = { <aKey>: <aValue>, ... }             python dictionary constructor

  aDictionary = { 'ida': '1', \                  
                  'idb': '2', \
                  'idc': '3', \
                  'idd': '3', \
                  'ide': '4', \
                  'idf': '4', \
                  'idg': '4'  \
                  }

# the OP used aDictionaryOfDictionaries construction instead.
# aDictOfDICTs = {'ida':{'key':'1'},'idb':{'key':'2'}, ... }

# The algorithmisation of the processing task is different.

# for aDictionary, a inverseDictionary may be assembled 
# to hold for each <aValue>,
# a referring <aKey>, incl. those, where are more than one primary <aKey>-s,
# ( stored asListOfPrimaryKEYs or aSetOfPrimaryKEYs ), which len() is easy to test

A fair & robust solution for a proper anInverseDICTIONARY construction can be found at "Fast functional solution for non-bijective maps (values not unique):" in >>> https://stackoverflow.com/a/22235665/3666197

Beware, not all posts yield a robust solution there.

Finally

>>> aListOfDUP_KEYs = [ anInvDictKEY for anInvDictKEY in anInverseDICTIONARY.keys() if ( len( anInverseDICTIONARY[anInvDictKEY] ) > 1 )  ]

    [ 3, 4 ]

''' as anInverseDICTIONARY = { 1: set( [ 'ida' ] ),
                               2: set( [ 'idb' ] ),
                               3: set( [ 'idd', 'idc' ] ),
                               4: set( [ 'idg', 'idf', 'ide' ] )
                               }
'''

Remove whichever DUP-e from anInverseDICTIONARY via aDUP_KEY, or from the original aDictionary by a list comprehension

>>> [ aDictionary.pop( anInverseDICTIONARY[aDUP_KEY].pop() ) for aDUP_KEY in aListOfDUP_KEYs ]

    [ 'idd', 'idf' ]


来源:https://stackoverflow.com/questions/25517504/how-to-get-list-of-keys-that-share-a-value-with-another-key-within-the-same-dict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!