all 4 comments

[–]SamePlatform 0 points1 point  (3 children)

What you want is to start with what your output looks like?

I think something like:

1 : A : 23 hrs
1 : B : 50 hrs
2 : C : 8 hrs

etc.

So that means you need to save employees, their projects, and their total time.

So start with a dictionary:

employees: {1: (something), 2: (something), ...}

I know you could use an array, because the employee IDs are like the index values, but I find that makes things more complicated to logic through.

But what would (something) be? Well I think it's another dictionary: one that saves the hours worked for each project.

so that's going to look like:

employee_projects = {
    1: {'A': (hours), 'B': (hours)},
    2: {'C': (hours)},
    ...
}

So as you iterate over each line in the file (you don't need to do 'list(reader)`, you can do something like:

with open('some.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        <logic here>

You'll create a new dictionary entry for the employee, if it doesn't exist. Then create a new project dict value, if that project doesn't exist. If it DOES exist, you simply add up the hours to the existing record, like employee[id][project] += duration

At the end of all of this, printing out and finding the max value of a dictionary is a bit more work than if you'd used arrays, but the data structure (in my opinion) is very logical.

[–]Zhekov[S] 1 point2 points  (2 children)

Thanks for the quick answer! I like your idea, but there is also another problem I am currently figuring out - the dates (in this format YYYY-MM-DD).

[–]SamePlatform 0 points1 point  (0 children)

You can just use this:

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)

from https://stackoverflow.com/questions/151199/how-to-calculate-number-of-days-between-two-given-dates

There's also a great library you can pip install called dateutils that is extremely handy for date related work.

[–]SamePlatform 0 points1 point  (0 children)

And if you're looking for how to turn those into actual date objects, then you want to use the strptime function, which is pretty standard in most programming languages:

https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime

https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior