简体   繁体   中英

Python dictionary add in value / optimize

I want to find the number of tags in a multidimensional array. I do it like this:

l['users'] is my data (array)

tags = {}
for u in l['users']:
    for p in u['photos']:
        for t in p['tags']:
            if tags.get(t):
                tags[t] +=1
            else:
                tags[t] = 1

Is there any cleaner or faster way to write that code?

How about this fast and pythonic one-liner solution using collections.Counter() :

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values.

Counter(t for u in l['users'] for p in u['photos'] for t in p['tags'])

DEMO:

from collections import Counter

l = {'users': [{'photos': [{'tags': [1,2,3,4,5]}, {'tags': [3,4,5]}]},
               {'photos': [{'tags': [1]}, {'tags': [2,3,4,5]}]}]}

tags = Counter(t for u in l['users'] for p in u['photos'] for t in p['tags'])
print tags  # prints Counter({3: 3, 4: 3, 5: 3, 1: 2, 2: 2})

Use a collections.defaultdict(int) , which will use 0 as the default value for any key that doesn't already have one:

import collections
tags = collections.defaultdict(int)
for u in l['users']:
    for p in u['photos']:
        for t in p['tags']:
            tags[t] +=1

Also, if tags.get(t) is a bad way to check whether t is a key in tags , especially since it'd fail in any context where a value could be considered false in a boolean context. Prefer the following:

if t in tags:

collections.Counter非常适合计算事物。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM