书生大模型实战营-Python 基础知识

任务一-单词统计:

import re
from collections import defaultdict

text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""

def wordcount(text):
    text.lower();
    # 将文本转换为小写
    text = text.lower()
    # 使用正则表达式将文本中的单词提取出来
    words = re.findall(r'\b\w+\b', text)
    # 使用defaultdict来存储单词的计数
    word_count = defaultdict(int)

    for word in words:
        word_count[word] += 1

    return dict(word_count)

print(wordcount(text))


任务二-远程Debug:

有兴趣可参加书生大模型实战营

posted @ 2024-07-18 10:03  紫叶&&  阅读(14)  评论(0)    收藏  举报