dict generator question

Gerard flanagan grflanagan at gmail.com
Fri Sep 19 14:37:44 CEST 2008


Boris Borcic wrote:
> Gerard flanagan wrote:
>> George Sakkis wrote:
> ..
>>>
>>> Note that this works correctly only if the versions are already sorted
>>> by major version.
>>>
>>
>> Yes, I should have mentioned it. Here's a fuller example below. 
>> There's maybe better ways of sorting version numbers, but this is what 
>> I do.
> 
> Indeed, your sort takes George's objection too litterally, what's needed 
> for a correct endresult is only that major versions be grouped together, 
> and this is most simply obtained by sorting the input data in (default) 
> string order, is it not ?
> 
>>
Yes, I see what you mean - the fact that a default sort orders "1.10" 
before "1.9" doesn't actually matter for the required result.
>>
>> datadict = \
>> dict((k, len(list(g))) for k,g in groupby(data, lambda s: s[:3]))

And, s[:3] is wrong. So:

data.sort()
datadict = \
dict((k, len(list(g))) for k,g in groupby(data, lambda s:
     '.'.join(s.split('.',2)[:2])))

should work, I hope.

Cheers,

Gerard




More information about the Python-list mailing list