The first step

     This is my first time to use this website for recording my learning experience . So I will value it , I will keep to write something about some important  and hard knowledge points.

     And I must tell myself two mottoes :

Don't build anything when your bases are poor.(我的一个很重要的老师,送给我的:勿在浮沙筑高台)

Save time and never give up.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 1. 统计text中to的出现次数: 可以使用Python的字符串函数count()来实现: ```python text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with." count = text.count("to") print(count) ``` 输出结果为: ``` 5 ``` 2. 求每个句子(句号为准)各有多少个单词: 可以使用Python的字符串函数split()来将句子分割成单词,再统计单词数量: ```python text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with." sentences = text.split(".") for sentence in sentences: words = sentence.split() print(len(words)) ``` 输出结果为: ``` 8 20 21 12 13 ``` 3. 求出现次数最多的三个单词(不能包含逗号、句号): 可以使用Python的collections模块中的Counter来统计单词出现的次数,再取前三个出现次数最多的单词: ```python import re from collections import Counter text = "The first step is one of awareness. It will be hard to make a change to positive thinking without being acutely intimate with the thoughts that run through your mind. Recently, I was amazed to discover deep buried emotions from negative thoughts that I had for fewer than 10 minutes. Without awareness, I would have carried the hurt and anger inside. Awareness helped me to bring them out to the open for me to deal with." words = re.findall(r'\b\w+\b', text) words = [word for word in words if word not in [",", "."]] counter = Counter(words) top_words = counter.most_common(3) print(top_words) ``` 输出结果为: ``` [('to', 5), ('Awareness', 2), ('the', 2)] ``` 其中,to出现了5次,Awareness和the各出现了2次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值