acclivity

pyMessageDict

Nov 26th, 2023
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | Software | 0 0
  1. data = {
  2.     'user_id': [1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1],
  3.     'message_text': [
  4.         'Hi, How are you?', 'I hope you remember me', 'I am fine.', 'how can I forgot you', 'that is great 🙂',
  5.         'so what is the plan?', 'I hope you will not back up',
  6.         'off course not', 'let me set a timing for it', 'ohh nice', 'thanks'
  7.     ]
  8. }
  9.  
  10. output = {"user_id": [], "message_text": []}
  11.  
  12. last_id = -1
  13. temp = []
  14. for x, id in enumerate(data["user_id"] + [9]):
  15.     if id != last_id:
  16.         if temp:
  17.             output["user_id"].append(last_id)
  18.             output["message_text"].append("\n".join(temp))
  19.         temp = []
  20.     if id == 9:
  21.         break
  22.     temp.append(data["message_text"][x])
  23.     last_id = id
  24.  
  25. print(output)
  26.  
  27. # Result: {'user_id': [1, 2, 1, 2, 1], 'message_text': ['Hi, How are you?\nI hope you remember me',
  28. # 'I am fine.\nhow can I forgot you', 'that is great 🙂\nso what is the plan?\nI hope you will not back up',
  29. # 'off course not\nlet me set a timing for it', 'ohh nice\nthanks']}
Advertisement
Add Comment
Please, Sign In to add comment