博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
coursesa课程 Python 3 programming Accumulating Multiple Results In a Dictionary 统计文件的字母数量
阅读量:4057 次
发布时间:2019-05-25

本文共 946 字,大约阅读时间需要 3 分钟。

f = open('scarlet.txt', 'r')txt = f.read()# now txt is one long string containing all the charactersx = {
} # start with an empty dictionaryfor c in txt: if c not in x: # we have not seen this character before, so initialize a counter for it x[c] = 0 #whether we've seen it before or not, increment its counter x[c] = x[c] + 1print("t: " + str(x['t']) + " occurrences")print("s: " + str(x['s']) + " occurrences")

题目:Provided is a string saved to the variable name sentence. Split the string into a list of words, then create a dictionary that contains each word and the number of times it occurs. Save this dictionary to the variable name word_counts.

sentence = "The dog chased the rabbit into the forest but the rabbit was too quick."words = sentence.split()word_counts = dict()for word in words:    if word not in word_counts:        word_counts[word] = 0    word_counts[word] = word_counts[word] +1print(word_counts)

转载地址:http://zirci.baihongyu.com/

你可能感兴趣的文章
C++报错:无法打开文件“路径\Debug\文件名.exe”
查看>>
【数据结构基础笔记】第二章线性表之单链表
查看>>
【积跬步以至千里】Excel行列互换
查看>>
【YOLO学习笔记】之YOLO初体验
查看>>
【YOLO学习笔记】之YOLO配置文件详解
查看>>
【YOLO学习笔记】之YOLO v1 论文笔记1(超详细:翻译+理解)
查看>>
【YOLO学习笔记】之YOLO v1 论文笔记2(超详细:翻译+理解)
查看>>
【YOLO学习笔记——数据集】之一YOLO数据集制作1(含LabelImg工具讲解)
查看>>
【积跬步以至千里】pdf转word后数字和英文格式别扭,无法修改
查看>>
【YOLO学习笔记——数据集】之一YOLO数据集制作2
查看>>
【深度学习小常识】CPU(中央处理器)和GPU(图像处理器)的区别
查看>>
【人工智能小常识】一篇文章搞懂人工智能、机器学习和深度学习
查看>>
【积跬步以至千里】如何查看浏览器保存的密码
查看>>
【opencv拓展】摄像头基本操作
查看>>
【数据结构周周练】001顺序表与链表(含上海大学832计算机组成原理与数据结构原题)
查看>>
C++获取数组长度(自写函数实现)
查看>>
C++报错:写入位置 0xCCCCCCCC 时发生访问冲突。
查看>>
C++报错:引发了未经处理的异常:写入访问权限冲突, p 是 0xCCCCCCCC
查看>>
【数据结构周周练】002顺序表与链表
查看>>
C++报错:C4700:使用了非初始化的局部变量
查看>>