Search code examples
pythoncsvcountingarcpy

How do I count the number of times a date shows up in a list?


So I am having an issue. I am trying to create a script that will look at a layer in ArcMap (we use 10.2.2) create a csv file that for the final output will have the technicians name and the number of tracts that they have looked at on a particular day. To do this, I think that I need to create a dictionary that is specific to each technician and has the date as a key and the number of times it comes up as the value.

This is where I am getting stuck. How do I create a dictionary that counts the number of times a date comes up?

I included my code so far and I am at a huge loss of where to go or if I am even on the right tract. Also I am unsure how to make the csv file with all three columns I need (Name, Date, Count).

import arcpy, datetime
from arcpy import mapping
from datetime import datetime

# pass layer to script
mxd = mapping.MapDocument("C:\\Users\\eschlueter\\Desktop\\test\\test.mxd")
layers = mapping.ListLayers(mxd)

inLayer = layers[0]
csvfilename = "C:\\Users\\eschlueter\\Desktop\\test\\test1.csv"

# Create the search cursor
cursor = arcpy.SearchCursor(inLayer)

##define a dictionary of every technician
elizabethtech = {}
unknowntech = {}

##create a counter for each technician's contracts
Elizabethcontract = 0
Unknowncontract = 0

##Define Variables and Iterate through the rows in cursor
desc = arcpy.Describe(inLayer)
fields =  desc.fields
for field in fields:
    for srow in cursor:
        tech = srow.getValue("Technician")
        ModDate = srow.getValue("ModifiedDate")
        if tech == "Elizabeth Schlueter":
            Elizabethcontract = Elizabethcontract + 1
            ESList = []
            ES = {}
            FormDate = ModDate.strftime("%m-%d-%Y")
            print FormDate
            ESList.append(FormDate)
            c = ESList.count(FormDate)
            ES[FormDate] = c
            print ES

I could really use some help on this one! Thank you guys for helping me in advance!

UPDATE

I have been able to create the dictionary using the Counter and the .update method and created a master list of all the technicians as the key and the value being the (date, count). How do I now take this dictionary and print a csv file? I want the file to have the technician name and the date and the count of the dates. Is this possible?


Solution

  • Look at the Counter class in the collections package. It's a type of dictionary where the keys will be dates (in your case), and the values will be the accumulated counts of how many times that date occurred. So for each employee you create a Counter instead of a dictionary. As you parse the data you collect the new dates for each employee and add them to the Counter (see Counter.update).

    from collections import Counter
    dates = ['5-11-15','5-12-15','5-11-15']
    c = Counter()
    c.update(dates) 
    print(c)
    

    This prints: Counter({'5-11-15': 2, '5-12-15': 1})