计算日历周并将它们存储到带有.isocalendar的数组中

时间:2015-10-19 14:39:24

标签: python arrays datetime

所以我得到了一本类似的字典:

{2321: datetime.datetime(2015, 2, 16, 11, 55, 50, 414175, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2322: datetime.datetime(2015, 2, 16, 13, 10, 17, 338086, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2323: datetime.datetime(2015, 2, 16, 13, 12, 30, 847941, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2324: datetime.datetime(2015, 2, 16, 13, 15, 14, 803438, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2325: datetime.datetime(2015, 2, 16, 13, 17, 42, 749529, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2326: datetime.datetime(2015, 2, 16, 13, 19, 58, 757024, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2327: datetime.datetime(2015, 2, 16, 13, 22, 16, 554052, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2328: datetime.datetime(2015, 2, 16, 13, 26, 56, 4452, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2081: datetime.datetime(2015, 1, 27, 10, 28, 10, 695887, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2082: datetime.datetime(2015, 1, 27, 10, 35, 34, 71091, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>), 
2083: datetime.datetime(2015, 1, 27, 10, 40, 1, 436955, tzinfo=<LocalTimezone "UTC+01:00" 1:00:00>)} 

其{ticketID:ticketDateTime}

我想使用.isocalendar从DateTime对象获取日历周期(因为我需要计算每周的票数)

我考虑过创建一个填充零长度为52的数组(因为一年有52周),然后用forloop遍历字典并且总是在这个日历周的索引中加1。

抱歉,我是python的新手,并会尝试用C#向您展示一些代码,我认为它可以起作用:

int[] resultsArray = new int[52]

for(int i = 0; i<= myDictionary.Length; i++){
index = myDictionary[i].isocalendar()[1]
resultsArray[index] = resultsArray[index]+1}

2 个答案:

答案 0 :(得分:0)

您可以使用Counter执行此操作。 https://docs.python.org/2/library/collections.html#counter-objects

c = Counter()
for d in dates:
    c[d.week] += 1  # note I don't know the syntax for week off the top of my head

答案 1 :(得分:0)

试试这个:

ClosedList = [0] * 53
OpenList = [0] * 53
OpenedList = [0] * 53
def CreateKWlist():
for i in ClosedDict:
    for j,k in ClosedDict[i]['prio-events']:
        if j.isocalendar()[0] == 2015:
            index = j.isocalendar()[1]
            ClosedList[index] += 1
            OpenedList[index] += 1
            break
for i in OpenDict:
    for j,k in OpenDict[i]['prio-events']:
        if j.isocalendar()[0] == 2015:
            index = j.isocalendar()[1]
            OpenList[index] += 1
            OpenedList[index] += 1
            break