您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

count_participations.py 774B

1234567891011121314151617181920212223
  1. from typing import List
  2. from million.model.message import Message
  3. from million.model.participant import Participant
  4. def count_participations(messages: List[Message], participants: List[Participant]):
  5. """
  6. Count the number of messages sent by each participant
  7. """
  8. participations = {}
  9. for participant in participants:
  10. participations[participant.name] = 0
  11. for message in messages:
  12. if message.sender_name not in participations:
  13. participations[message.sender_name] = 1
  14. else:
  15. participations[message.sender_name] += 1
  16. ordered_participations = sorted(
  17. participations.items(), key=lambda x: x[1], reverse=True)
  18. return [{"name": v[0], "participations": v[1]} for v in ordered_participations]