简体   繁体   English

计算日历周数,并使用.isocalendar将其存储到数组中

[英]Counting calendar weeks and store them into an array with .isocalendar

so I got a dictionary which looks like this: 所以我得到了一个像这样的字典:

{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>)} 

its {ticketID:ticketDateTime} 其{ticketID:ticketDateTime}

i'd like to use .isocalendar to get calendarweeks from the DateTime-objects (because i need to count tickets for every week) 我想使用.isocalendar从DateTime对象获取calendarweeks(因为我需要每周计算票数)

i've thought about creating an array filled with zeros with the length 52 (because a year has 52 weeks), then iterate through the dictionary with a forloop and always add 1 to the index of this calendarweek. 我考虑过要创建一个长度为52的零填充数组(因为一年有52周),然后使用forloop遍历字典,并始终向该日历周的索引添加1。

sorry, I am new in python and will try to show you some code in C#, how i think it could work: 抱歉,我是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}

You can use a Counter to do this. 您可以使用Counter来执行此操作。 https://docs.python.org/2/library/collections.html#counter-objects 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

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM