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!
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']
]
]
]
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']
]
]
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With