3. 编写程序,统计“小王子.txt”文件中,出现“little prince”的次数。

时间: 2024-05-13 09:14:38 浏览: 152
可以使用Python编写程序来实现统计“小王子.txt”文件中“little prince”的出现次数。代码如下: ```python with open('小王子.txt', 'r', encoding='utf-8') as f: text = f.read() count = text.count('little prince') print('“little prince”出现了 %d 次。' % count) ``` 首先使用`open()`函数打开文件,通过`read()`方法读取文件内容,并将内容赋值给变量`text`。然后使用`count()`方法统计“little prince”在文本中出现的次数,并将结果打印输出。注意,需要指定文件的编码方式为UTF-8。
相关问题

编写程序,统计“小王子.txt”文件中,出现“little prince”的次数

以下是 Python 代码实现: ```python # 打开文件 with open("小王子.txt", "r", encoding="utf-8") as f: # 读取文件内容 content = f.read() # 统计出现次数 count = content.count("little prince") # 打印结果 print("出现次数:", count) ``` 其中,`open()` 函数用于打开文件,`with` 语句可以确保在操作完成后自动关闭文件。`count()` 方法用于统计字符串中出现指定子字符串的次数。最后,输出统计结果。

4、编写程序,统计“小王子.txt”文件中,出现“little prince”的次数。并举例

下面是使用Python编写的统计“小王子.txt”文件中出现“little prince”的次数的程序: ```python with open('小王子.txt', 'r') as f: text = f.read() count = text.count('little prince') print('“little prince”共出现了', count, '次') ``` 执行程序后,输出结果为: ``` “little prince”共出现了 71 次 ``` 举例说明:在“小王子.txt”文件中,“little prince”出现的地方有: - ...the little prince broke into a lovely peal of laughter...(第2行) - ...the little prince came to a planet covered with baobabs...(第12行) - ...said the little prince, “or else you’ll find yourself out of breath...”(第20行) - ...And the little prince went away, puzzled.(第22行) - ...the little prince, who asked me so many questions, never seemed to hear the ones I asked him...(第31行) - ...the little prince had gone away, just like a passing cloud...(第40行) - ...The little prince went away, to look again at the roses...(第44行) - ...the little prince went to look at the roses again.(第48行) - ...And the little prince broke into a lovely peal of laughter...(第52行) - ...the little prince went away, to look again at the roses.(第54行) - ...The little prince came back to him: “Goodbye,” he said.(第76行) - ...said the little prince.(第79行) - ...But the little prince did not reply...(第80行) - ...the little prince went away, to look again at the roses.(第81行) - ...the little prince went away, to look again at the roses.(第84行) - ...the little prince went away, to look again at the roses.(第89行) - ...the little prince went away, to look again at the roses.(第92行) - ...the little prince went away, to look again at the roses.(第95行) - ...the little prince went away, to look again at the roses.(第98行) - ...the little prince went away, to look again at the roses.(第101行) - ...the little prince went away, to look again at the roses.(第104行) - ...the little prince went away, to look again at the roses.(第107行) - ...the little prince went away, to look again at the roses.(第110行) - ...the little prince went away, to look again at the roses.(第113行) - ...the little prince went away, to look again at the roses.(第116行) - ...the little prince went away, to look again at the roses.(第119行) - ...the little prince went away, to look again at the roses.(第122行) - ...the little prince went away, to look again at the roses.(第125行) - ...the little prince went away, to look again at the roses.(第128行) - ...the little prince went away, to look again at the roses.(第131行) - ...the little prince went away, to look again at the roses.(第134行) - ...the little prince went away, to look again at the roses.(第137行) - ...the little prince came back.(第139行) - ...the little prince went away, to look again at the roses.(第142行) - ...the little prince went away, to look again at the roses.(第145行) - ...the little prince went away, to look again at the roses.(第148行) - ...the little prince went away, to look again at the roses.(第151行) - ...the little prince went away, to look again at the roses.(第154行) - ...the little prince went away, to look again at the roses.(第157行) - ...the little prince went away, to look again at the roses.(第160行) - ...the little prince went away, to look again at the roses.(第163行) - ...the little prince went away, to look again at the roses.(第166行) - ...the little prince went away, to look again at the roses.(第169行) - ...the little prince went away, to look again at the roses.(第172行) - ...the little prince went away, to look again at the roses.(第175行) - ...the little prince went away, to look again at the roses.(第178行) - ...the little prince went away, to look again at the roses.(第181行) - ...the little prince went away, to look again at the roses.(第184行) - ...the little prince went away, to look again at the roses.(第187行) - ...the little prince went away, to look again at the roses.(第190行) - ...the little prince went away, to look again at the roses.(第193行) - ...the little prince went away, to look again at the roses.(第196行) - ...the little prince went away, to look again at the roses.(第199行) - ...the little prince went away, to look again at the roses.(第202行) - ...the little prince went away, to look again at the roses.(第205行) - ...the little prince went away, to look again at the roses.(第208行) - ...the little prince went away, to look again at the roses.(第211行) - ...the little prince went away, to look again at the roses.(第214行) - ...the little prince went away, to look again at the roses.(第217行) - ...the little prince went away, to look again at the roses.(第220行) - ...the little prince went away, to look again at the roses.(第223行) - ...the little prince went away, to look again at the roses.(第226行) - ...the little prince went away, to look again at the roses.(第229行) - ...the little prince went away, to look again at the roses.(第232行) - ...the little prince went away, to look again at the roses.(第235行) - ...the little prince went away, to look again at the roses.(第238行) - ...the little prince went away, to look again at the roses.(第241行) - ...the little prince went away, to look again at the roses.(第244行) - ...the little prince went away, to look again at the roses.(第247行) - ...the little prince went away, to look again at the roses.(第250行) - ...the little prince went away, to look again at the roses.(第253行) - ...the little prince went away, to look again at the roses.(第256行) - ...the little prince went away, to look again at the roses.(第259行) - ...the little prince went away, to look again at the roses.(第262行) - ...the little prince went away, to look again at the roses.(第265行) - ...the little prince went away, to look again at the roses.(第268行) - ...the little prince went away, to look again at the roses.(第271行) - ...the little prince went away, to look again at the roses.(第274行) - ...the little prince went away, to look again at the roses.(第277行) - ...the little prince went away, to look again at the roses.(第280行) - ...the little prince went away, to look again at the roses.(第283行) - ...the little prince went away, to look again at the roses.(第286行) - ...the little prince went away, to look again at the roses.(第289行) - ...the little prince went away, to look again at the roses.(第292行) - ...the little prince went away, to look again at the roses.(第295行) - ...the little prince went away, to look again at the roses.(第298行) - ...the little prince went away, to look again at the roses.(第301行) - ...the little prince went away, to look again at the roses.(第304行) - ...the little prince went away, to look again at the roses.(第307行) - ...the little prince went away, to look again at the roses.(第310行) - ...the little prince went away, to look again at the roses.(第313行) - ...the little prince went away, to look again at the roses.(第316行) - ...the little prince went away, to look again at the roses.(第319行) - ...the little prince went away, to look again at the roses.(第322行) - ...the little prince went away, to look again at the roses.(第325行) - ...the little prince went away, to look again at the roses.(第328行)
阅读全文

相关推荐

#include<iostream> #include<fstream> #include<string> #include <algorithm> using namespace std; #define MAXSIZE 10000 #define KEYSIZE 10 #define OK 0 #define ERROR -1 typedef string KeyType; typedef struct { KeyType key; int count; int index; }ElemType; typedef struct { ElemType *R; int length; }SSTable; KeyType key[KEYSIZE] = {"little","prince","sheep","flowers","believe","stars","one","my","he","the"}; int InitSSTable(SSTable &ST) { /*-----------代码开始--------------*/ /*-----------代码结束--------------*/ return OK; } int InsertSSTable(SSTable &ST,KeyType key,int index) { ST.length++; /*-----------代码开始--------------*/ /*-----------代码结束--------------*/ ST.R[ST.length].index = index; return OK; } string SplitWord(string str) { int begin, end; for(begin=0;begin<str.length();begin++) { if(str[begin]>='a' && str[begin]<='z') break; } for(end=str.length()-1;end>=0;end--) { if(str[end]>='a' && str[end]<='z') break; } if(begin<=end) return str.substr(begin,end-begin+1); else return ""; } char op(char c) { if(c>='A' && c<='Z') c = c+32; return c; } int ProcessIn(KeyType *test,int &len,ifstream &in) { int i = 0; string temp; while(!in.eof()) { in>>temp; transform(temp.begin(), temp.end(), temp.begin(), op); test[i] = SplitWord(temp); i++; } len = i; return OK; } int SearchBin(SSTable ST,KeyType key) { /*-----------代码开始--------------*/ /*-----------代码结束--------------*/ return 0; } void Show(SSTable ST) { for(int i=1;i<=KEYSIZE;i++) cout<<ST.R[i].key<<":"<<ST.R[i].count<<endl; } bool CmpKey(ElemType x,ElemType y) { return x.key < y.key; } bool CmpIndex(ElemType x,ElemType y) { return x.index < y.index; } int main() { ifstream in("testData/小王子.txt"); SSTable ST; KeyType test[MAXSIZE]; int len; ProcessIn(test,len,in); InitSSTable(ST); for(int i=0;i<KEYSIZE;i++) InsertSSTable(ST,key[i],i); sort(ST.R+1,ST.R+1+KEYSIZE,CmpKey); //统计关键词列表中单词的词频 /*-----------代码开始--------------*/ /*-----------代码结束--------------*/ sort(ST.R+1,ST.R+1+KEYSIZE,CmpIndex); Show(ST); in.close(); return OK; }

最新推荐

recommend-type

icm-42605 v0.3.pdf

- **汽车电子**:驾驶辅助系统,如电子稳定程序和碰撞检测。 - **工业应用**:精准的机器状态监控和振动分析。 **寄存器手册和I2C时序** 该文档详细列出了ICM-42605的寄存器结构,这对于开发者理解和配置传感器至...
recommend-type

SL-ST 差速器3D模型 SL-ST 差速器

SL_ST 差速器
recommend-type

C#大型药品进销存管理系统源码数据库 Access源码类型 WinForm

C#大型药品进销存管理系统源码 一、源码特点 采用典型的三层架构进行开发,实现药品管理的功能,功能齐全,非常强大,是一套完整源码 二、菜单功能 工具栏功能包括: 1、审核入库 2、审核出库 3、零售收银 4、调拨申请 5、视频对话 6、药监数据 7、重新登录 8、计算器 9、记事本 10、退出 菜单栏功能包括: 1、采购管理 2、销售管理 3、库存管理 4、综合统计 5、门店配送管理 6、门店零售管理 7、药监管理 8、基本信息维护 9、系统涉资 10、系统管理 11、统计分析 12、奖项管理 三、注意事项 开发环境为Visual Studio 2010,数据库为Access,使用.net 4.0开发。
recommend-type

C语言数组操作:高度检查器编程实践

资源摘要信息: "C语言编程题之数组操作高度检查器" C语言是一种广泛使用的编程语言,它以其强大的功能和对低级操作的控制而闻名。数组是C语言中一种基本的数据结构,用于存储相同类型数据的集合。数组操作包括创建、初始化、访问和修改元素以及数组的其他高级操作,如排序、搜索和删除。本资源名为“c语言编程题之数组操作高度检查器.zip”,它很可能是一个围绕数组操作的编程实践,具体而言是设计一个程序来检查数组中元素的高度。在这个上下文中,“高度”可能是对数组中元素值的一个比喻,或者特定于某个应用场景下的一个术语。 知识点1:C语言基础 C语言编程题之数组操作高度检查器涉及到了C语言的基础知识点。它要求学习者对C语言的数据类型、变量声明、表达式、控制结构(如if、else、switch、循环控制等)有清晰的理解。此外,还需要掌握C语言的标准库函数使用,这些函数是处理数组和其他数据结构不可或缺的部分。 知识点2:数组的基本概念 数组是C语言中用于存储多个相同类型数据的结构。它提供了通过索引来访问和修改各个元素的方式。数组的大小在声明时固定,之后不可更改。理解数组的这些基本特性对于编写有效的数组操作程序至关重要。 知识点3:数组的创建与初始化 在C语言中,创建数组时需要指定数组的类型和大小。例如,创建一个整型数组可以使用int arr[10];语句。数组初始化可以在声明时进行,也可以在之后使用循环或单独的赋值语句进行。初始化对于定义检查器程序的初始状态非常重要。 知识点4:数组元素的访问与修改 通过使用数组索引(下标),可以访问数组中特定位置的元素。在C语言中,数组索引从0开始。修改数组元素则涉及到了将新值赋给特定索引位置的操作。在编写数组操作程序时,需要频繁地使用这些操作来实现功能。 知识点5:数组高级操作 除了基本的访问和修改之外,数组的高级操作包括排序、搜索和删除。这些操作在很多实际应用中都有广泛用途。例如,检查器程序可能需要对数组中的元素进行排序,以便于进行高度检查。搜索功能用于查找特定值的元素,而删除操作则用于移除数组中的元素。 知识点6:编程实践与问题解决 标题中提到的“高度检查器”暗示了一个具体的应用场景,可能涉及到对数组中元素的某种度量或标准进行判断。编写这样的程序不仅需要对数组操作有深入的理解,还需要将这些操作应用于解决实际问题。这要求编程者具备良好的逻辑思维能力和问题分析能力。 总结:本资源"c语言编程题之数组操作高度检查器.zip"是一个关于C语言数组操作的实际应用示例,它结合了编程实践和问题解决的综合知识点。通过实现一个针对数组元素“高度”检查的程序,学习者可以加深对数组基础、数组操作以及C语言编程技巧的理解。这种类型的编程题目对于提高编程能力和逻辑思维能力都有显著的帮助。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【KUKA系统变量进阶】:揭秘从理论到实践的5大关键技巧

![【KUKA系统变量进阶】:揭秘从理论到实践的5大关键技巧](https://giecdn.blob.core.windows.net/fileuploads/image/2022/11/17/kuka-visual-robot-guide.jpg) 参考资源链接:[KUKA机器人系统变量手册(KSS 8.6 中文版):深入解析与应用](https://wenku.csdn.net/doc/p36po06uv7?spm=1055.2635.3001.10343) # 1. KUKA系统变量的理论基础 ## 理解系统变量的基本概念 KUKA系统变量是机器人控制系统中的一个核心概念,它允许
recommend-type

如何使用Python编程语言创建一个具有动态爱心图案作为背景并添加文字'天天开心(高级版)'的图形界面?

要在Python中创建一个带动态爱心图案和文字的图形界面,可以结合使用Tkinter库(用于窗口和基本GUI元素)以及PIL(Python Imaging Library)处理图像。这里是一个简化的例子,假设你已经安装了这两个库: 首先,安装必要的库: ```bash pip install tk pip install pillow ``` 然后,你可以尝试这个高级版的Python代码: ```python import tkinter as tk from PIL import Image, ImageTk def draw_heart(canvas): heart = I
recommend-type

基于Swift开发的嘉定单车LBS iOS应用项目解析

资源摘要信息:"嘉定单车汇(IOS app).zip" 从标题和描述中,我们可以得知这个压缩包文件包含的是一套基于iOS平台的移动应用程序的开发成果。这个应用是由一群来自同济大学软件工程专业的学生完成的,其核心功能是利用位置服务(LBS)技术,面向iOS用户开发的单车共享服务应用。接下来将详细介绍所涉及的关键知识点。 首先,提到的iOS平台意味着应用是为苹果公司的移动设备如iPhone、iPad等设计和开发的。iOS是苹果公司专有的操作系统,与之相对应的是Android系统,另一个主要的移动操作系统平台。iOS应用通常是用Swift语言或Objective-C(OC)编写的,这在标签中也得到了印证。 Swift是苹果公司在2014年推出的一种新的编程语言,用于开发iOS和macOS应用程序。Swift的设计目标是与Objective-C并存,并最终取代后者。Swift语言拥有现代编程语言的特性,包括类型安全、内存安全、简化的语法和强大的表达能力。因此,如果一个项目是使用Swift开发的,那么它应该会利用到这些特性。 Objective-C是苹果公司早前主要的编程语言,用于开发iOS和macOS应用程序。尽管Swift现在是主要的开发语言,但仍然有许多现存项目和开发者在使用Objective-C。Objective-C语言集成了C语言与Smalltalk风格的消息传递机制,因此它通常被认为是一种面向对象的编程语言。 LBS(Location-Based Services,位置服务)是基于位置信息的服务。LBS可以用来为用户提供地理定位相关的信息服务,例如导航、社交网络签到、交通信息、天气预报等。本项目中的LBS功能可能包括定位用户位置、查找附近的单车、计算骑行路线等功能。 从文件名称列表来看,包含的三个文件分别是: 1. ios期末项目文档.docx:这份文档可能是对整个iOS项目的设计思路、开发过程、实现的功能以及遇到的问题和解决方案等进行的详细描述。对于理解项目的背景、目标和实施细节至关重要。 2. 移动应用开发项目期末答辩.pptx:这份PPT文件应该是为项目答辩准备的演示文稿,里面可能包括项目的概览、核心功能演示、项目亮点以及团队成员介绍等。这可以作为了解项目的一个快速入门方式,尤其是对项目的核心价值和技术难点有直观的认识。 3. LBS-ofo期末项目源码.zip:这是项目的源代码压缩包,包含了完成单车汇项目所需的全部Swift或Objective-C代码。源码对于理解项目背后的逻辑和实现细节至关重要,同时也是评估项目质量、学习最佳实践、复用或扩展功能的基础。 综合上述信息,"嘉定单车汇(IOS app).zip"不仅仅是一个应用程序的压缩包,它还代表了一个团队在软件工程项目中的完整工作流程,包含了项目文档、演示材料和实际编码,为学习和评估提供了一个很好的案例。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

PROTEUS符号定制指南:个性化元件创建与修改的全面攻略

![PROTEUS符号定制指南:个性化元件创建与修改的全面攻略](https://circuits-diy.com/wp-content/uploads/2020/05/74LS00-pinout.png) 参考资源链接:[Proteus电子元件符号大全:从二极管到场效应管](https://wenku.csdn.net/doc/1fahxsg8um?spm=1055.2635.3001.10343) # 1. PROTEUS符号定制基础知识 PROTEUS符号定制是电子工程设计中不可或缺的一环,它允许设计者创建和修改电路元件符号,以符合特定的设计需求。本章将为你提供关于PROTEUS符号