Home > Blockchain >  how to remove key value pairs from a dictionary based on values which are in list format and having
how to remove key value pairs from a dictionary based on values which are in list format and having

Time:06-24

mydict= { '5963': [94, 622], '5501': [94, 335], '1724': [95, 450], '1674': [96, 1037], '6369': [96, 941], '380': [96, 271],'6468': [132, 701], '630': [132, 140], '3530': [133, 513]}

I want to eject those key value pairs where values have any common element with one other. I've tried this:

new_dict = { k:v for seen in [set()]
             for k,v in sorted(mydict.items(),key=lambda kv:-len(kv[1]))
                 if not (seen.issuperset(v),seen.update(v))[0] }

but this doesnt show desired answer.

CodePudding user response:

In the following code I reject all key-value pairs, whose value has at least one number in common with another value of a key-value pair.

mydict= {'5963': [94, 622], 
         '5501': [94, 335], 
         '1724': [95, 450], 
         '1674': [96, 1037], 
         '6369': [96, 941], 
         '380':  [96, 271],
         '6468': [132, 701], 
         '630':  [132, 140], 
         '3530': [133, 513]}

import itertools
from collections import Counter

# get a list of all number in values
all_values = list(itertools.chain.from_iterable(mydict.values()))
>>> [94, 622, 94, 335, 95, 450, 96, 1037, 96, 941, 96, 271, 132, 701, 132, 140, 133, 513]

# count each value occurence
count = Counter(all_values)
>>> Counter({96: 3, 94: 2, 132: 2, 622: 1, 335: 1, 95: 1, 450: 1, 1037: 1, 941: 1, 271: 1, 701: 1, 140: 1, 133: 1, 513: 1})

# select values in common
exclude_values = set([k for k, v in count.items() if v > 1])
>>> {96, 132, 94}

# exclude key, value in dict if one number in value list is in exclude_values 
mydict = {k: v for k, v in mydict.items() if set(v).isdisjoint(exclude_values)}
>>> {'1724': [95, 450], '3530': [133, 513]}

The only case not managed here is the case where a number appears twice in the same value. In this case you have to modify the creation of all_values to count only once a number appearing twice in the same value. One solution is to convert lists into sets:

all_values = list(itertools.chain.from_iterable([set(v) for v in mydict.values()]))
  • Related