dict generator question

Simon Mullis simon at mullis.co.uk
Thu Sep 18 11:56:08 EDT 2008


Haha!

Thanks for all of the suggestions... (I love this list!)

SM

2008/9/18  <pruebauno at latinmail.com>:
> On Sep 18, 10:54 am, "Simon Mullis" <si... at mullis.co.uk> wrote:
>> Hi,
>>
>> Let's say I have an arbitrary list of minor software versions of an
>> imaginary software product:
>>
>> l = [ "1.1.1.1", "1.2.2.2", "1.2.2.3", "1.3.1.2", "1.3.4.5"]
>>
>> I'd like to create a dict with major_version : count.
>>
>> (So, in this case:
>>
>> dict_of_counts = { "1.1" : "1",
>>                    "1.2" : "2",
>>                    "1.3" : "2" }
>>
>> 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.
>>
>> I'm most probably not thinking pythonically enough... (I know I could
>> do this pretty easily with a couple more lines, but I'd like to
>> understand if there's a way to use a dict generator for this).
>>
>> Thanks in advance
>>
>> SM
>>
>> --
>> Simon Mullis
>
> Considering 3 identical "simultpost" solutions I'd say:
>  "one obvious way to do it" FTW :-)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Simon Mullis
_________________
simon at mullis.co.uk



More information about the Python-list mailing list