python - Find an item in a dictionary value, in another dictionary value with a length of 1 -


for every item in dictionary value len > 1, searching item in dictionary value len == 1. if find item in dictionary value len == 1, want remove longer value. example:

d = {      'key1' : ['one', 'two', 'three'],     'key2' : ['one'],     'key3' : ['two', 'three'],     } 

should return

{  'key1' : ['two', 'three'],  'key2' : ['one'],  'key3' : ['two', 'three'], } 

my current code this

allvals = match.values()  k, v in match.iteritems():      dontuse = []     newval = []      in v:         x in allvals:             if x == v:                 pass             elif in x:                 if len(x) == 1:                     dontuse.append(i)     in v:         if in dontuse:             pass         else:             newval.append(i)      match[k] = list(set(newval)) 

however extreme bottleneck processing time. appreciated, thanks!

it's little difficult interpret trying believe can break 2 steps:

  • create set of items remove
  • remove items lists len > 1

both of these can done comprehensions (set, dict), e.g.:

>>> d = { 'key1' : ['one', 'two', 'three'], 'key2' : ['one'], 'key3' : ['two', 'three']} >>> r = {v[0] k, v in d.items() if len(v) == 1} >>> {k: [v v in vs if v not in r] if len(vs) > 1 else vs k, vs in d.items()} {'key1': ['two', 'three'], 'key2': ['one'], 'key3': ['two', 'three']} 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -