no data exclution and unique combination.

Hans Mulder hansmu at xs4all.nl
Fri Aug 10 04:47:02 EDT 2012


On 9/08/12 23:33:58, Terry Reedy wrote:
> On 8/9/2012 4:06 PM, giuseppe.amatulli at gmail.com wrote:
[...]
>> unique=dict()
>> for row in  array2D :
>>      row = tuple(row)
>>      if row in unique:
>>          unique[row] += 1
>>      else:
>>          unique[row] = 1
> 
> I believe the 4 lines above are equivalent to
>    unique[row] = unique.get(row, 0) + 1


I would write that bit as:

from collections import defaultdict

unique = defaultdict(int)
for row in  array2D:
    unique[row] += 1


Hope this helps,

-- HansM



More information about the Python-list mailing list