Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = {
- 'user_id': [1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1],
- 'message_text': [
- 'Hi, How are you?', 'I hope you remember me', 'I am fine.', 'how can I forgot you', 'that is great 🙂',
- 'so what is the plan?', 'I hope you will not back up',
- 'off course not', 'let me set a timing for it', 'ohh nice', 'thanks'
- ]
- }
- output = {"user_id": [], "message_text": []}
- last_id = -1
- temp = []
- for x, id in enumerate(data["user_id"] + [9]):
- if id != last_id:
- if temp:
- output["user_id"].append(last_id)
- output["message_text"].append("\n".join(temp))
- temp = []
- if id == 9:
- break
- temp.append(data["message_text"][x])
- last_id = id
- print(output)
- # Result: {'user_id': [1, 2, 1, 2, 1], 'message_text': ['Hi, How are you?\nI hope you remember me',
- # 'I am fine.\nhow can I forgot you', 'that is great 🙂\nso what is the plan?\nI hope you will not back up',
- # 'off course not\nlet me set a timing for it', 'ohh nice\nthanks']}
Advertisement
Add Comment
Please, Sign In to add comment