# 单词出现频次统计 string = "to be or not to be that is a question" # 定义变量存储字符串 lis = string.split(" ") # 使用分割函数将字符串转为列表 count = {} # 定义一个空字典 for x in lis: # 遍历列表 if x not in count: # 判断列表元素是否在字典中 count[x] = 1 # 如果元素还不在字典中,那么就添加键值对 else: count[x] += 1 # 如果元素已经在字典中,那么就将键对应的值加1 print(count) # 打印字典