【入门岛·第2关】python基础

三法师 / 2024-09-25 / 原文

目录
  • Python实现wordcount
  • Vscode连接InternStudio debug笔记

Python实现wordcount

import string

def wordcount(text):
    # 去掉标点符号,并将文本转换为小写
    text = text.translate(str.maketrans('', '', string.punctuation)).lower()
    
    # 按空格分割文本为单词
    words = text.split()
    
    # 统计单词出现的次数
    count_dict = {}
    for word in words:
        count_dict[word] = count_dict.get(word, 0) + 1
        
    return count_dict

# 示例输入
input_text = """Hello world!  
This is an example.  
Word count is fun.  
Is it fun to count words?  
Yes, it is fun!"""

# 输出结果
output = wordcount(input_text)
print(output)

Vscode连接InternStudio debug笔记

  1. 打三个断点

  2. 逐个运行断点代码,注意到第二个断点处的变量变化
    第一次进入循环
    image
    每一次循环变量的变化
    image
    完成循环之后的output和terminal中打印的一致
    image