S1M0N_H
S1M0N_H

Reputation: 139

Tuple of tuples to list or dict

I'm writing a job planner and the following is an sqlite3 query result. What I'm wanting is to group the data by engineer (e.g. 'Dan') and whether they have any jobs (the last column) or are on holiday (the 2nd to last column).

So ideally I'd probably want a dict with a key for each engineer, and a key for each day, and either:

I've tried the following

jobs = {}
for j in res:
    jobs.setdefault(j[0], []).append(j[1:])


jobs2 = {}
for j in res:
    jobs2.setdefault((j[0], j[1]), []).append(j[2:])

but can't figure out how to use either setdefault or a dict comprehension to get the data structure I need. Any ideas appreciated.

Here's the data:

('Dan', '2021-03-11', 'Thu', '2021-03-11', 'am', '11/03', 'Customer 1', '', 'U6kfoP9QPDw', None, 0, 1)                                                                                                                                 
('Dan', '2021-03-12', 'Fri', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                                  
('Dan', '2021-03-13', 'Sat', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                                  
('Dan', '2021-03-14', 'Sun', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                                  
('Dan', '2021-03-15', 'Mon', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                                  
('Dan', '2021-03-16', 'Tue', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                                  
('Dan', '2021-03-17', 'Wed', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                                  
('Gareth', '2021-03-11', 'Thu', None, None, None, None, None, None, None, 0, 0)                                                                                                                                                               
('Gareth', '2021-03-12', 'Fri', '2021-03-12', 'am', '12/03', 'Customer 4', '', 'k-uFnkwLLdo', None, 0, 2)                                                                                                                              
('Gareth', '2021-03-12', 'Fri', '2021-03-12', 'pm', '12/03', 'Customer 2', '', 'TWQdiG3piAE', None, 0, 2)
('Gareth', '2021-03-13', 'Sat', None, None, None, None, None, None, None, 0, 0)                                        
('Gareth', '2021-03-14', 'Sun', None, None, None, None, None, None, None, 0, 0)                                        
('Gareth', '2021-03-15', 'Mon', None, None, None, None, None, None, None, 0, 0)                                        
('Gareth', '2021-03-16', 'Tue', None, None, None, None, None, None, None, 0, 0)                                        
('Gareth', '2021-03-17', 'Wed', None, None, None, None, None, None, None, 0, 0)                                        
('Garth', '2021-03-11', 'Thu', None, None, None, None, None, None, None, 0, 0)                                         
('Garth', '2021-03-12', 'Fri', None, None, None, None, None, None, None, 0, 0)                                         
('Garth', '2021-03-13', 'Sat', None, None, None, None, None, None, None, 0, 0)                                         
('Garth', '2021-03-14', 'Sun', None, None, None, None, None, None, None, 0, 0)                                         
('Garth', '2021-03-15', 'Mon', None, None, None, None, None, None, None, 0, 0)                                         
('Garth', '2021-03-16', 'Tue', None, None, None, None, None, None, None, 0, 0)                                         
('Garth', '2021-03-17', 'Wed', None, None, None, None, None, None, None, 0, 0)                                         
('Ian', '2021-03-11', 'Thu', '2021-03-11', 'am', '11/03', 'Customer 3', '', 'SCfbuLeQ9ig', None, 0, 1)
('Ian', '2021-03-12', 'Fri', None, None, None, None, None, None, None, 0, 0)                                           
('Ian', '2021-03-13', 'Sat', None, None, None, None, None, None, None, 0, 0)                                           
('Ian', '2021-03-14', 'Sun', None, None, None, None, None, None, None, 0, 0)                                           
('Ian', '2021-03-15', 'Mon', None, None, None, None, None, None, None, 0, 0)                                           
('Ian', '2021-03-16', 'Tue', None, None, None, None, None, None, None, 0, 0)                                           
('Ian', '2021-03-17', 'Wed', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-11', 'Thu', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-12', 'Fri', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-13', 'Sat', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-14', 'Sun', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-15', 'Mon', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-16', 'Tue', None, None, None, None, None, None, None, 0, 0)                                           
('Jim', '2021-03-17', 'Wed', None, None, None, None, None, None, None, 1, 0)                    

Upvotes: 0

Views: 70

Answers (2)

S1M0N_H
S1M0N_H

Reputation: 139

Thanks to the tip from Reti43, I've now ended up with this:

jobs3 = {}
for nick, date, *job in res:
    jobs3.setdefault(nick, {}).setdefault(date, {}).setdefault('jobs', [])
    if job[-2]:
        jobs3[nick][date].update({'holiday': job[-3]})
    if job[-1]:
        jobs3[nick][date]['jobs'].append(job[2:-2])

Upvotes: 0

Reti43
Reti43

Reputation: 9796

It seems a dict of dicts will suffice. The outer dict will have as key the employee name and value the inner dict, which will have the dates as keys and jobs/status as values.

ON_HOLIDAY = None

jobs = {}
for employee, date, *job in res:
    if employee not in jobs:
        jobs[employee] = dict()
    if date not in jobs[employee]:
        jobs[employee][date] = []
    if job[-2]:
        jobs[employee][date] = ON_HOLIDAY
    else:
        if job[-1]:
            jobs[employee][date].append(job[2:-2])

An empty list signifies the employee has no jobs on that date, but you can also change that with another identifier once you have parsed all your data.

Upvotes: 1

Related Questions