On 08May2019 21:04, Dave Hill <d...@the-hills.org.uk> wrote:
I have a csv file which details the results of equipment tests, I carry out PAT testing as a volunteer at a heriatge railway in N. Wales. I want to extract how many items were tested on each test day. So far I have generated a List of test dates, but I am now stalled at how to efficiently count numbers tested on each date.

Can I have a list of tuples, where one item is the date and the second the count?

Not as such, because you can't modify a tuple (so you can't update the count part). But you could use a 2 element list.

or is there a better construct?

Oh definitely. The easiest thing would be a defaultdict(int). Example:

 from collections import defaultdict
 ...
 by_date = defaultdict(int)
 for row in csvdata:
   timestamp = row[1]  # based on your example data
   # get the date from the timestamp
   date = ...
   by_date[date] += 1

A defaultdict is a dict which magicly makes missing elements when they get access, using a factory function you supply. Here we're using "int" as that factory, as int() returns zero.

I presume you've got the timestamp => date conversion sorted?

Cheers,
Cameron Simpson <c...@cskk.id.au>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to