【问题标题】:Python- Count number of occurrences of a date in a listPython-计算列表中日期的出现次数
【发布时间】:2016-01-09 05:01:10
【问题描述】:

我有一个缺陷列表,其中包含缺陷日期、缺陷的优先​​级、缺陷所在的 sprint 以及缺陷所在的月份。我想计算优先级 1、2、3 的数量和总数列表中每个日期的缺陷。 目前我正在使用它来识别总缺陷,但这种逻辑似乎不起作用。如果有人可以帮助我。

import datetime
#this is my defect list 
defectdetails = 
[[datetime.datetime(2015, 1, 1, 0, 0), 1, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 3, 0, 0), 2, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 6, 0, 0), 1, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 10, 0, 0), 1, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 16, 0, 0), 2, 'Sprint 2', 'January 2015'], [datetime.datetime(2015, 2, 18, 0, 0), 3, 'Sprint 4', 'February 2015'], [datetime.datetime(2015, 3, 3, 0, 0), 1, 'Sprint 5', 'March 2015'], [datetime.datetime(2015, 3, 7, 0, 0), 1, 'Sprint 5', 'March 2015'], [datetime.datetime(2015, 3, 9, 0, 0), 3, 'Sprint 5', 'March 2015'], [datetime.datetime(2015, 4, 5, 0, 0), 1, 'Sprint 7', 'April 2015'], [datetime.datetime(2015, 4, 15, 0, 0), 2, 'Sprint 7', 'April 2015'], [datetime.datetime(2015, 4, 25, 0, 0), 1, 'Sprint 8', 'April 2015'], [datetime.datetime(2015, 5, 9, 0, 0), 2, 'Sprint 9', 'May 2015'], [datetime.datetime(2015, 5, 14, 0, 0), 3, 'Sprint 9', 'May 2015'], [datetime.datetime(2015, 5, 19, 0, 0), 2, 'Sprint 10', 'May 2015'], [datetime.datetime(2015, 5, 21, 0, 0), 3, 'Sprint 10', 'May 2015'], [datetime.datetime(2015, 6, 1, 0, 0), 1, 'Sprint 11', 'June 2015'], [datetime.datetime(2015, 6, 5, 0, 0), 1, 'Sprint 11', 'June 2015'], [datetime.datetime(2015, 7, 15, 0, 0), 2, 'Sprint 14', 'July 2015'], [datetime.datetime(2015, 7, 25, 0, 0), 1, 'Sprint 14', 'July 2015'], [datetime.datetime(2015, 8, 8, 0, 0), 1, 'Sprint 15', 'August 2015'], [datetime.datetime(2015, 8, 19, 0, 0), 3, 'Sprint 16', 'August 2015'], [datetime.datetime(2015, 8, 19, 0, 0), 2, 'Sprint 16', 'August 2015'], [datetime.datetime(2015, 8, 20, 0, 0), 1, 'Sprint 16', 'August 2015'],  [datetime.datetime(2015, 11, 12, 0, 0), 3, 'Sprint 22', 'November 2015'], [datetime.datetime(2015, 11, 21, 0, 0), 3, 'Sprint 22', 'November 2015'], [datetime.datetime(2015, 12, 11, 0, 0), 1, 'Sprint 23', 'December 2015'], [datetime.datetime(2015, 12, 30, 0, 0), 1, 'Sprint 25', 'December 2015'], [datetime.datetime(2015, 1, 1, 0, 0), 1, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 3, 0, 0), 2, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 1, 0, 0), 1, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 3, 0, 0), 3, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 1, 0, 0), 2, 'Sprint 1', 'January 2015'], [datetime.datetime(2015, 1, 3, 0, 0), 2, 'Sprint 1', 'January 2015']]


defectdetailscopy = list(defectdetails)

for i in range(len(defectdetails)):
    value = len(defectdetailscopy)
    for j in range(0,value,1):
         print(j)
         if (defectdetails[i][0] == defectdetailscopy[j][0]):
              count +=1
              defectdetailscopy.pop(j)
              value = len(defectdetailscopy)
     print ('the total defect for date' + str(defectdetails[i][0]) +'is '+str(count)) 

当第二个循环运行时,它会引发我的索引超出范围错误。我相信不知何故我在 if 条件中更新的变量值不适用于 for 循环,因此当我从列表中弹出元素时,循环失败并出现 index out of bound 错误。

【问题讨论】:

  • 为什么不使用字典将日期作为键并将该日期的缺陷列表作为值而不是列表?
  • 嗨@Pynchia,谢谢。我会在我的代码中尝试一下,看看字典是如何工作的。

标签: python list datetime count counter


【解决方案1】:

您可以使用 collections.Counter 和生成器表达式 -

from collections import Counter
dcounts = Counter(d[0] for d in defectdetails)
for d, count in dcounts.items():
    print('The total defects for date {} is {}'.format(d, count))

您还可以使用datetime.datetime.strftime() 方法来格式化您打印日期的方式。示例(以DD-MM-YYY 格式打印日期)打印语句将变为 -

 print('The total defects for date {} is {}'.format(d.strftime('%d-%m-%Y'), count))

使用您的数据进行演示 -

>>> from collections import Counter
>>> dcounts = Counter(d[0] for d in defectdetails)
>>> for d, count in dcounts.items():
...     print('The total defects for date {} is {}'.format(d, count))
...
The total defects for date 2015-01-16 00:00:00 is 1
The total defects for date 2015-08-19 00:00:00 is 2
The total defects for date 2015-03-09 00:00:00 is 1
The total defects for date 2015-03-07 00:00:00 is 1
The total defects for date 2015-07-15 00:00:00 is 1
The total defects for date 2015-05-19 00:00:00 is 1
The total defects for date 2015-03-03 00:00:00 is 1
The total defects for date 2015-01-10 00:00:00 is 1
The total defects for date 2015-04-25 00:00:00 is 1
The total defects for date 2015-06-05 00:00:00 is 1
The total defects for date 2015-05-21 00:00:00 is 1
The total defects for date 2015-02-18 00:00:00 is 1
The total defects for date 2015-11-21 00:00:00 is 1
The total defects for date 2015-05-14 00:00:00 is 1
The total defects for date 2015-12-30 00:00:00 is 1
The total defects for date 2015-07-25 00:00:00 is 1
The total defects for date 2015-05-09 00:00:00 is 1
The total defects for date 2015-11-12 00:00:00 is 1
The total defects for date 2015-04-05 00:00:00 is 1
The total defects for date 2015-01-03 00:00:00 is 4
The total defects for date 2015-04-15 00:00:00 is 1
The total defects for date 2015-01-01 00:00:00 is 4
The total defects for date 2015-06-01 00:00:00 is 1
The total defects for date 2015-08-08 00:00:00 is 1
The total defects for date 2015-08-20 00:00:00 is 1
The total defects for date 2015-01-06 00:00:00 is 1
The total defects for date 2015-12-11 00:00:00 is 1

【讨论】:

  • 嗨,这对于缺陷总数来说非常有效。如果我需要更复杂的东西,比如日期 01-01-2015 我有 4 个总缺陷,其中我有 2 个作为优先级 1、1 个作为优先级 2 和 1 作为优先级 3。我如何在同一个柜台。我看了几个例子没有发现什么好东西。如果我的逻辑正确,我会在这里发布
  • 您可以在计数器内使用tuple 作为键,然后您将获得该元组的计数。元组可以是日期和优先级。
【解决方案2】:

A.您还没有初始化计数。

B.当您从defectdetailscopy 中弹出元素时,您正在减少defectdetailscopy 的长度。因此,当您将 defectdetailscopy 与 if 条件下的 defectdetails 进行比较时,它会引发 index out of bound 错误。也就是说,当 j 达到 30 时,defectdetailscopy 中的元素数量为 30。所以当你这样做时:

defectdetails[i][0] == defectdetailscopy[30][0]

列表中没有第 30 个元素。只到索引 29。

更好的方法是使用字典。

我试过了:

date_wise_stats = {}
priority_list = {}
>>> for i in defectdetails:
...     if i[0] in date_wise_stats:
...             date_wise_stats[i[0]] += 1
...     else:
...             date_wise_stats[i[0]] = 1
...     if i[1] in priority_list:
...             priority_list[i[1]] += 1
...     else:
...             priority_list[i[1]] = 1   

所以我得到以下结果:

将日期时间格式化为字符串后:

{'2015-08-08': 1, '2015-05-21': 1, '2015-05-09': 1, '2015-01-10': 1, '2015-07-15': 1, '2015-01-16': 1, '2015-11-12': 1, '2015-08-20': 1, '2015-04-05': 1, '2015-04-25': 1, '2015-05-19': 1, '2015-05-14': 1, '2015-08-19': 2, '2015-07-25': 1, '2015-01-06': 1, '2015-01-03': 4, '2015-11-21': 1, '2015-01-01': 4, '2015-03-09': 1, '2015-02-18': 1, '2015-03-03': 1, '2015-03-07': 1, '2015-12-30': 1, '2015-06-01': 1, '2015-12-11': 1, '2015-06-05': 1, '2015-04-15': 1}

>>> priority_list
{1: 16, 2: 10, 3: 8}

【讨论】:

    【解决方案3】:

    您可以使用列表推导或集合中的 counter/defaultdict 模块将其缩小,但我认为这是它的核心。

    result = {}
    
    # count it
    for d in defectdetails:
    
        #init the a node for the day
        if d[0] not in result:
            result[d[0]] = {1:0,2:0,3:0}
    
        result[d[0]][d[1]] += 1
    
    # report it
    for d,defects in sorted(result.items()):
        print("%s, 1: %d, 2: %d, 3: %d, total: %d" % (
            d,
            defects[1],
            defects[2],
            defects[3],
            sum(n for n in defects.values()))
            )
    

    【讨论】:

      猜你喜欢
      • 2015-07-24
      • 1970-01-01
      • 2021-09-09
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多