Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping elements time-wise in a date-wise nested list?

I have a nested list that has this type of structure:

mylist = [

[
    ['Bob', 'Male', '2019-12-10 9:00'], 
    ['Sally', 'Female', '2019-12-10 15:00']
], 

[
    ['Jake', 'Male', '2019-12-12 9:00'], 
    ['Ally', 'Female', '2019-12-12 9:30'], 
    ['Jamal', 'Male', '2019-12-12 15:00']
], 

[
    ['Andy', 'Male', '2019-12-13 15:00'],
    ['Katie', 'Female', '2019-12-13 15:30']
]

]

Currently, this master list mylist is organized by date. All elements containing the same day (i.e. 2019-12-12, 2019-12-13...) are nested together.

I'd like to take this nesting one step further and create another nested group inside that date-wise nested group. This time, I'd like to organize them time-wise. I would like to group all people that have a tag at hour 9 together, and people with a tag at hour 15 together.

So, I'm trying to get this output:

newlist = [

[
  [
    ['Bob', 'Male', '2019-12-10 9:00']
  ], 
  [
    ['Sally', 'Female', '2019-12-10 15:00']
  ]
], 


[
  [
    ['Jake', 'Male', '2019-12-12 9:00'], 
    ['Ally', 'Female', '2019-12-12 9:30'], 
  ],
  [
    ['Jamal', 'Male', '2019-12-12 15:00']
  ]
], 


[
  [
    ['Andy', 'Male', '2019-12-13 15:00'],
    ['Katie', 'Female', '2019-12-13 15:30']
  ]
]


]

Based on the accepted answer to this question, I modified and tried to use the following code, but it didn't work.

newdict = defaultdict(list)

for data in mylist:
    for datum in data:
        _, _, time = datum
        _, date_time = time.split(" ")
        _, hour_minute = date_time.split(":")
        newdict[hour_minute].append(datum)

newlist = list(newdict.values())
print(newlist)

--------------------------------------------------------------------------------------------------------
Output:

[
  [
     ['Bob', 'Male', '2019-12-10 9:00'], 
     ['Sally', 'Female', '2019-12-10 15:00'], 
     ['Jake', 'Male', '2019-12-12 9:00'], 
     ['Ally', 'Female', '2019-12-12 9:30'], 
     ['Jamal', 'Male', '2019-12-12 15:00'], 
     ['Andy', 'Male', '2019-12-13 15:00'],
     ['Katie', 'Female', '2019-12-13 15:30']
  ]
]

I've also done a lot of online searching and couldn't find any solutions. Does anyone know how to accomplish this? Also, please keep in mind that I'm new to programming, so please try to keep answers/explanations as simple as possible. Thanks!

like image 727
F16Falcon Avatar asked Oct 16 '22 08:10

F16Falcon


2 Answers

If they're already in order, we can use groupby with a function to isolate the hour to group them:

from itertools import groupby


def f(l):
    return l[2].split(" ")[1].split(":")[0]

newlist = [[list(g) for _, g in groupby(subl, f)] for subl in mylist]

results in

[
  [
    [
      ['Bob', 'Male', '2019-12-10 9:00']
    ],
    [ 
      ['Sally', 'Female', '2019-12-10 15:00']
    ]
  ],
  [
    [
      ['Jake', 'Male', '2019-12-12 9:00'], 
      ['Ally', 'Female', '2019-12-12 9:30']
    ],
    [
      ['Jamal', 'Male', '2019-12-12 15:00']
    ]
  ],
  [
    [
      ['Andy', 'Male', '2019-12-13 15:00'],
      ['Katie', 'Female', '2019-12-13 15:30']
    ]
  ]
]
like image 100
Patrick Haugh Avatar answered Oct 20 '22 15:10

Patrick Haugh


You can use itertools.groupby:

from itertools import groupby as gb
def group_l(d): 
  return [list(b) for _, b in gb(_d, key=lambda x:x[-1].split()[-1].split(':')[0]))]


result = list(map(group_l, mylist))

Output:

[
 [
   [
     ['Bob', 'Male', '2019-12-10 9:00']
   ], 
   [
     ['Sally', 'Female', '2019-12-10 15:00']
   ]
 ], 
 [
   [
    ['Jake', 'Male', '2019-12-12 9:00'], 
    ['Ally', 'Female', '2019-12-12 9:30']
   ], 
   [['Jamal', 'Male', '2019-12-12 15:00']]
  ], 
 [
   [
     ['Andy', 'Male', '2019-12-13 15:00'], 
     ['Katie', 'Female', '2019-12-13 15:30']
   ]
  ]
 ]
like image 42
Ajax1234 Avatar answered Oct 20 '22 16:10

Ajax1234