python - 查找我出现最多的特殊字符
问题描述
我有一个任务,我需要计算字符串中出现最多的特殊字符。
我拥有的文件包含三个带有文本的段落,我需要将它们相互比较以查看一组字符出现的位置,然后计算它们出现最多的位置。
文本中的行:
'I cannot go now. Give me lunch first at 12:15.'
After 13:100, he took a nap for until 13:15. Then in the late afternoon on 2018-11:30 at 16:30, he picked some bags and went to the palace. On the way, he felt hot so he sat under a tree to rest. Then, two hours later at 18:30, he got up to go but saw a man showing some magic tricks. He stopped to watch for an until 21:04.
When he reached the palace it was already after 21:03. The palace gates had been shut. So Haria had lost a golden chance because he had not learned the value of time on the 2018-13-01, a beautiful day.
我到目前为止的代码:
number_of_specials = 0
c = ['.', '.', ';', ':', '!', '?']
top = []
top_c1 = 0
top_c2 = 0
top_c3 = 0
with open(TEXT, "r") as fh:
for line in fh:
top.append(line.split("\n")) //to get the lines
if c in top[0]:
top_c1 += 1
不知道从哪里开始,任何指针将不胜感激
解决方案
尝试这个:
import re
c = ['.', '.', ';', ':', '!', '?']
top = ['asdd..,;;.:']
top_len = len(re.findall('['+''.join(c)+']',top[0]))
推荐阅读
- javascript - 反应子组件不从父值渲染道具
- javascript - Javascript:计算时差不会返回任何问题
- json - django ajax 内容安全策略指令
- jupyter-notebook - 风筝不在 jupyter 实验室工作,但在 python 工作
- php - PHP & Bootstrap 3 sendmail.php 确认进入引导提示成功或失败消息
- powershell - 添加“获取模块 | 移除模块;Clear-Host' 到包含的 PowerShell 代码
- reactjs - 如何解决 React js 中的解析错误?
- java - 我可以在springboot中轻松重用url路径映射作为设置吗?
- amazon-web-services - 将负载均衡器添加到 Elastic Beanstalk 会导致 502 Bad Gateway
- r - R:如何改变列并将其分组