dict generator question

Paul Rubin http
Fri Sep 19 08:10:31 CEST 2008


"Simon Mullis" <simon at mullis.co.uk> writes:
> l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
> ...
> dict_of_counts = dict([(v[0:3], "count") for v in l])

Untested:

    def major_version(version_string):
       "convert '1.2.3.2' to '1.2'"
       return '.'.join(version_string.split('.')[:2])

    dict_of_counts = defaultdict(int)

    for x in l:
       dict_of_counts[major_version(l)] += 1



More information about the Python-list mailing list