Slice level with glom

76 Views Asked by At

I have a nested structure like

target = \
{'A1':
   {'B1': 'a1b1',
    'B2': 'a1b2'
   }
{'A2':
   {'B1': 'a2b1',
    'B2': 'a2b2'
   }
}

how can I easily find all the items which have 'B2' in the second level (pandas terminology) i.e. ['a1b2', 'a2b2']?

I tried

glom(target, T[:, 'B2'])

glom(target, Path(Path(), Path('B2')))
1

There are 1 best solutions below

0
Vijay Hebbar On BEST ANSWER

I'm assuming the target you specified is a dictionary like so -

import glom
from glom import Coalesce, SKIP

target = \
{'A1':
   {'B1': 'a1b1',
    'B2': 'a1b2'
   },
'A2':
   {'B1': 'a2b1',
    'B2': 'a2b2'
   },
'A3':
   {'B1': 'a2b1',
   }
}

# We don't care about the outer key in the dictionary
# so we get only the inner dictionary by using values()

data = target.values()

# In the spec, we access the path "B2". 
# Coalesce allows us to skip the item if it does not contain "B2"

spec = [Coalesce("B2",default=SKIP)]

print(list(glom.glom(data,spec)))

# ['a1b2', 'a2b2']