0. 任务:在 InternStudio 环境中实现功能:
- python 实现 wordcount函数,统计英文字符串单词的使用频率,并返回字典;
- vscode 远程调试 InternStudio 中的 python 代码
1. wordcount 函数实现
- string.punctuation 是一个字符串,它包含了所有的ASCII标点符号字符。
- 使用 maketrans() 方法创建映射表;
 如果字典/表格中没有指定字符,则不会替换该字符。
 如果使用字典,则必须使用 ascii 代码而不是字符。
- translate 方法返回一个字符串,其中某些指定字符被替换为字典或映射表中描述的字符。
- str.split(str=“”, num=string.count(str)).
 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
实现代码如下:
import string ,re
def wordcount(text):
    """
    wordcount 函数,统计英文字符串中每个单词出现的次数。
    返回一个字典,key为单词,value为对应单词出现的次数。
    """
    # 小写
    text = text.lower()
    
    # 去除所有的标点符号,只保留字母和空格
    translator = str.maketrans('', '', string.punctuation)
    
    # 使用 translate() 方法去除标点符号	
    text = text.translate(translator)
    
    # 拆分字符串为单词列表
    words = text.split()
    
    # 创建一个空字典用于存储单词出现次数
    word_count_dict = {}
    for word in words:
        if word in word_count_dict:
            word_count_dict[word] += 1
        else:
            word_count_dict[word] = 1
    return word_count_dict
if __name__ == '__main__':
    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.
    """
    print(wordcount(text))
    print('end')
结果如下:
 
2. VSCode 连接创建开发机并调试
VSCode 如何远程连接开发机器,可以参考其他教程;
 连接成功并调试的截图如下:
 
总体来说,属于 VSCode 远程开发时必备的技能。



















