'Group python list of dictionaries by multiple contiguous values
I currently have a text conversation as a list of dictionaries in the form:
conversation = [
{sender:"name1", message:"String1...", time:"2022-01-27 10:30:00 UTC"},
{sender:"name1", message:"String2...", time:"2022-01-27 10:30:30 UTC"},
{sender:"name2", message:"String3...", time:"2022-01-27 10:35:00 UTC"}
{sender:"name1", message:"String4...", time:"2022-01-27 10:36:00 UTC"}
{sender:"name1", message:"String5...", time:"2022-01-27 10:40:00 UTC"}
]
What I want to do is group the messages so that if they were sent by the same person and within 1 minute of the previous message they are grouped. What is the best way to do this?
For example, in the list shown above I would want String1 and String2 to be grouped into 1 message, because they were sent by the same person within the same minute, the rest of the list would then stay the same, because the sender is either different to the previous message or the messages are more than 1 minute apart.
Although ideally I would like them grouped by both sender and time, it would also be fine if someone could just show me how to just group contiguous messages with the same sender, no matter when they were sent.
I am using Python 3.9 if that makes any difference
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
