Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- Count number of occurrences of a date in a list

I have a defectlist with defect date, the priority of the defect and which sprint the defect is in and also the month-year the defect is present in. I want to count the number of priority 1,2,3 and total defects for every date in the list. Currently I am using this to identify the total defects but this logic does not seem to work. If someone can help me on this.

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

It throws me index out of bound error when the 2nd loop is running. I believe somehow the variable value which I am updating in the if condition does not apply to the for loop, due to which when I pop out the element from the list, the loop fails with index out of bound error.

like image 259
shiva1791 Avatar asked Oct 11 '15 18:10

shiva1791


2 Answers

You can use collections.Counter for this along with a generator expression -

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

You can also use datetime.datetime.strftime() method to format how you print the date . Example (for printing date in DD-MM-YYY format) the print statement would become -

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

Demo with your data -

>>> 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
like image 198
Anand S Kumar Avatar answered Oct 10 '22 19:10

Anand S Kumar


A. You haven't initialised count.

B. When you are popping element from defectdetailscopy, you are reducing the length of defectdetailscopy. So when you compare defectdetailscopy with defectdetails in if condition, it throws index out of bound error. That is when j reaches 30, number of elements in defectdetailscopy are 30. So when you do :

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

There is no 30th element present in the list. It is only till index 29.

A better way to do this is to use dictionary.

I tried this:

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   

So I get the following results:

After formatting datetime into string:

{'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}
like image 42
blackmamba Avatar answered Oct 10 '22 20:10

blackmamba