(no title)
abuckenheimer | 7 years ago
target = {'system': {'planets': [{'name': 'earth', 'moons': 1},
{'name': 'jupiter', 'moons': 69}]}}
glom(target, {'moon_count': ('system.planets', ['moons'], sum)})
# vs
def iter_moons(t):
for planet in target['system']['planets']:
yield planet['moons']
sum(iter_moons(target))
would have to combine with `defaultdict`s if your nested data is only sometimes there though
jeremiahwv|7 years ago
divbzero|7 years ago
mhashemi|7 years ago
ziikutv|7 years ago
>>> sum([x['moons'] for x in target['system']['planets']])