frequency of values in a field

noydb jenn.duerr at
Wed Feb 9 18:52:27 CET 2011

> The Decimal module is pretty slow but is conceptually probably the right
> way to do this.  With just 50k records it shouldn't be too bad.  With
> more records you might look for a faster way.
>     from decimal import Decimal as D
>     from collections import defaultdict
>     records = ['3.14159','2.71828','3.142857']
>     td = defaultdict(int)
>     for x in records:
>         td[D(x).quantize(D('0.01'))] += 1
>     print td

I played with this - it worked.  Using Python 2.6 so counter no good.

I require an output text file of sorted "key value" so I added
(further code to write out to an actual textfile, not important here)
>> for z in sorted(set(td)):
>>     print z, td[z]

So it seems the idea is to add all the records in the particular field
of interest into a list (record).  How does one do this in pure
Normally in my work with gis/arcgis sw, I would do a search cursor on
the DBF file and add each value in the particular field into a list
(to populate records above).  Something like:

>> import arcgisscripting
>> # Create the geoprocessor object
>> gp = arcgisscripting.create()
>> records_list = []
>> cur = gp.SearchCursor(dbfTable)
>> row = cur.Next()
>> while row:
>>    value = row.particular_field
>>    records_list.append(value)

More information about the Python-list mailing list