[Tutor] Nested dictionary with defaultdict

Kent Johnson kent37 at tds.net
Thu Apr 17 12:27:55 CEST 2008


Kepala Pening wrote:
> count = lambda x: [{y: x.count(y)} for y in set(x)]

At least for long enough lists, this is likely to be more expensive than 
the approach using defaultdict. Your count() function iterates the list 
(1+m) times, where m is the number of distinct words - once to create 
the set, and once for each call to x.count(). The complexity of your 
count() is O(m*n) where n is len(x).

OTOH the defaultdict method is O(n). The loops in your solution are all 
in C code which may give some performance improvement but I expect that 
would be overshadowed by the factor of m as m and n get large.

Kent

> y = {}
> for key, val in myDict.items():
>     y[key] = count(val)
> 
> print y
> 
> {'1': [{'220': 3}], '3': [{'238': 1}, {'220': 1}], '2': [{'238': 4}, {'220': 
> 1}], '5': [{'238': 1}, {'220': 2}], '4': [{'220': 2}], '7': [{'220': 1}], 
> '6': [{'238': 2}]}



More information about the Tutor mailing list