Home > Software engineering >  Group list of objects to sublists python
Group list of objects to sublists python

Time:08-04

I am trying to implement a code that group elements inside of list based on if start hour is different than last object. I would like to turn this list:

lst = [
            {
                "start": "2022-08-03T09:00:00Z",
                "end": "2022-08-03T17:00:00Z"
            },
            {
                "start": "2022-08-04T09:00:00Z",
                "end": "2022-08-04T17:00:00Z"
            },
            {
                "start": "2022-08-05T15:30:00Z",
                "end": "2022-08-05T23:30:00Z"
            },
            {
                "start": "2022-08-07T22:00:00Z",
                "end": "2022-08-08T06:00:00Z"
            }
        ]

Into this list of sublists:

separated_lst = [ 
    [
            {
                "start": "2022-08-03T09:00:00Z",
                "end": "2022-08-03T17:00:00Z"
            },
            {
                "start": "2022-08-04T09:00:00Z",
                "end": "2022-08-04T17:00:00Z"
            },
    ],
    [
            {
                "start": "2022-08-05T15:30:00Z",
                "end": "2022-08-05T23:30:00Z"
            },
    ],
    [
            {
                "start": "2022-08-07T22:00:00Z",
                "end": "2022-08-08T06:00:00Z"
            }
    ]
]

For now I implemented something that works but looks bad in my opinion:

separated_shifts = [[]]
        previous_shift = None
        counter = 0
        for shift in shifts:
            shift_start = self._format_date(shift["start"])
            if self.__is_different_shift(shift_start, previous_shift):
                counter  = 1
                separated_shifts.append([])
            separated_shifts[counter].append(shift)
            previous_shift = shift_start

CodePudding user response:

You can group the dictionaries to a dictionary with the hour as keys, and take the values of this dictionary

d = {}
for item in lst:
    d.setdefault(item['start'].split('T')[1], []).append(item)
separated_lst = list(d.values())

CodePudding user response:

In case the times might include minutes and seconds, just take the hour from the start time. As the values are all well-formatted date/time strings we can just slice them.

lst = [
            {
                "start": "2022-08-03T09:00:00Z",
                "end": "2022-08-03T17:00:00Z"
            },
            {
                "start": "2022-08-04T09:00:00Z",
                "end": "2022-08-04T17:00:00Z"
            },
            {
                "start": "2022-08-05T15:30:00Z",
                "end": "2022-08-05T23:30:00Z"
            },
            {
                "start": "2022-08-07T22:00:00Z",
                "end": "2022-08-08T06:00:00Z"
            }
        ]

d = {}

for e in lst:
    hh = e['start'][11:13]
    d.setdefault(hh, []).append(e)

separated_list = list(d.values())

print(separated_list)

Output:

[[{'start': '2022-08-03T09:00:00Z', 'end': '2022-08-03T17:00:00Z'}, {'start': '2022-08-04T09:00:00Z', 'end': '2022-08-04T17:00:00Z'}], [{'start': '2022-08-05T15:30:00Z', 'end': '2022-08-05T23:30:00Z'}], [{'start': '2022-08-07T22:00:00Z', 'end': '2022-08-08T06:00:00Z'}]]
  • Related