如何计算日期在列表中出现的次数? [英] How do I count the number of times a date shows up in a list?

查看:74
本文介绍了如何计算日期在列表中出现的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题.我正在尝试创建一个脚本,该脚本将查看ArcMap中的图层(我们使用10.2.2),创建一个csv文件,该文件的最终输出将包含技术人员的姓名以及他们在特定日期查看的区域数.为此,我认为我需要创建一个针对每个技术人员的字典,并以日期作为键,并以出现的次数作为值.

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?

到目前为止,我已经包含了我的代码,即使去了正确的地方,我也无所适从.另外我不确定如何用我需要的所有三列(名称,日期,计数)制作csv文件.

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!

更新

我已经能够使用Counter和.update方法创建字典,并创建了所有技术人员的主列表作为键,而值是(日期,计数).我现在该如何使用这本词典并打印一个csv文件?我希望文件具有技术员姓名,日期和日期计数.这可能吗?

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?

推荐答案

查看collections包中的Counter类.这是一种字典,其中的键将是日期(在您的情况下),而值将是该日期发生了多少次的累积计数.因此,为每个员工创建一个Counter而不是一个Dictionary.解析数据时,您将收集每个员工的新日期并将其添加到Counter(请参见Counter.update).

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)

此打印:计数器({'5-11-15':2,'5-12-15':1})

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

这篇关于如何计算日期在列表中出现的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆