From b8edf93df062196e2c14af97c5c11129254bd52b Mon Sep 17 00:00:00 2001 From: pg78ac9nr <2401677463@qq.com> Date: Wed, 18 Sep 2024 20:45:19 +0800 Subject: [PATCH] ADD file via upload --- 弹幕排名.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 弹幕排名.py diff --git a/弹幕排名.py b/弹幕排名.py new file mode 100644 index 0000000..fd36a1c --- /dev/null +++ b/弹幕排名.py @@ -0,0 +1,9 @@ +def top_danmakus(keyword_counts, top_n=8): + """ + 返回出现次数最多的前N个弹幕。 + :param keyword_counts: 弹幕及其出现次数的字典 + :param top_n: 排行榜的长度 + :return: 前N个弹幕及其出现次数 + """ + sorted_counts = sorted(keyword_counts.items(), key=lambda item: item[1], reverse=True) + return dict(sorted_counts[:top_n]) \ No newline at end of file