Simon Mullis napisaƂ(a):
> Something like:
>
> dict_of_counts = dict([(v[0:3], "count") for v in l])
>
> I can't seem to figure out how to get "count", as I cannot do x += 1
> or x++ as x may or may not yet exist, and I haven't found a way to
> create default values.

It seems to me that the "count" you're looking for is the number of
elements from l whose first 3 characters are the same as the v[0:3]
thing. So you may try:
>>> dict_of_counts = dict((v[0:3], sum(1 for x in l if x[:3] == v[:3])) for v 
>>> in l)

But this isn't particularly efficient. The 'canonical way' to
construct such histograms/frequency counts in python is probably by
using defaultdict:
>>> dict_of_counts = collections.defaultdict(int)
>>> for x in l:
>>>     dict_of_counts[x[:3]] += 1

Regards,
Marek
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to