Python- 计算一个日期在列表中出现的次数
Python- Count number of occurrences of a date in a list
我有一个缺陷列表,其中包含缺陷日期、缺陷的优先级、缺陷所在的 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 循环,因此当我从列表中弹出元素时,循环失败并出现索引超出范围错误。
您可以使用列表理解或集合中的 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()))
)
一个。你还没有初始化计数。
乙。当您从 defectdetailscopy 中弹出元素时,您正在减少 defectdetailscopy 的长度。因此,当您在 if 条件下将 defectdetailscopy 与 defectdetails 进行比较时,它会抛出索引超出范围的错误。即当 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}
您可以为此使用 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
我有一个缺陷列表,其中包含缺陷日期、缺陷的优先级、缺陷所在的 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 循环,因此当我从列表中弹出元素时,循环失败并出现索引超出范围错误。
您可以使用列表理解或集合中的 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()))
)
一个。你还没有初始化计数。
乙。当您从 defectdetailscopy 中弹出元素时,您正在减少 defectdetailscopy 的长度。因此,当您在 if 条件下将 defectdetailscopy 与 defectdetails 进行比较时,它会抛出索引超出范围的错误。即当 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}
您可以为此使用 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