我有一个缺陷清单,包括缺陷日期,缺陷的优先级,缺陷的冲刺时间,以及缺陷存在的月份。我要计算列表中每个日期的优先级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循环,因此当我从列表中弹出元素时,循环会失败,索引超出绑定错误。
发布于 2015-10-11 19:45:21
答:您还没有初始化计数。
当你从缺陷细节中弹出元素时,你正在减少缺陷细节的长度。因此,在if条件下,当您比较缺陷细节和缺陷细节时,它会将索引抛出界限误差。也就是说,当j达到30时,缺陷细节中的元素数为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}发布于 2015-10-11 19:48:21
可以将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发布于 2015-10-11 19:16:58
您可以通过列表理解或集合中的计数器/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()))
)https://stackoverflow.com/questions/33068875
复制相似问题