【学习笔记】大模型实战-L02-Python

L02 Python

题目要求

一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。

代码实现

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):
    # 将文本按行分割
    lines = text.strip().split('\n')
    word_dict = {}
    
    # 遍历每一行
    for line in lines:
        # 将每行按空格分割成单词
        words = line.split()
        for word in words:
            # 将单词转换为小写
            word = word.lower()
            # 统计单词出现次数
            if word in word_dict:
                word_dict[word] += 1
            else:
                word_dict[word] = 1
    
    return word_dict

# 测试函数
input_text = """Hello world!  
This is an example.  
Word count is fun.  
Is it fun to count words?  
Yes, it is fun!"""

print(wordcount(input_text))

远程调试

使用本地vscode连接远程开发机,将上面代码在开发机上进行调试。
远程连接方法见前一篇笔记。

在这里插入图片描述

Reference

https://github.com/InternLM/Tutorial/blob/camp3/docs/L0/Python/task.md

相关推荐

  1. 模型学习笔记07——模型之Adaptation

    2024-07-23 03:14:04       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 03:14:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 03:14:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 03:14:04       45 阅读
  4. Python语言-面向对象

    2024-07-23 03:14:04       55 阅读

热门阅读

  1. 【Apollo学习笔记】—— Cyber RT之创建组件, test ok

    2024-07-23 03:14:04       16 阅读
  2. Linux Shell 022-按日期清理文件

    2024-07-23 03:14:04       16 阅读
  3. Red Playing Cards (牛客多校2 I)

    2024-07-23 03:14:04       17 阅读
  4. Husky 入门

    2024-07-23 03:14:04       16 阅读
  5. ResNeSt

    ResNeSt

    2024-07-23 03:14:04      19 阅读
  6. 如何引入全局样式文件?

    2024-07-23 03:14:04       16 阅读
  7. 长短期记忆网络(LSTM)及其Python和MATLAB实现

    2024-07-23 03:14:04       19 阅读
  8. python的open()函数

    2024-07-23 03:14:04       12 阅读
  9. 【过题记录】 7.22

    2024-07-23 03:14:04       14 阅读
  10. linux kernel 内核缓存回收的相关配置项

    2024-07-23 03:14:04       17 阅读
  11. Asp Net Web API 请求报错

    2024-07-23 03:14:04       12 阅读
  12. 欧鹏 数据库第二次作业

    2024-07-23 03:14:04       13 阅读