Python-从大型文本文件中检索特定句子

Python-从大型文本文件中检索特定句子,python,list,count,frequency,Python,List,Count,Frequency,这是我的一句话: s = "& how are you then? I am fine, % and i want to found some food #meat with vegetable# #tea# #cake# and #tea# so on." 我想计算句子s中受#约束的单词的频率 我想要以下输出 [("meat with vegetable", 1) ("tea", 2) ("cake", 1)] 非常感谢你的帮助和时间 使用re和计数器的功能,可以轻松完成此任务

这是我的一句话:

s = "& how are you then? I am fine, % and i want to found some food #meat with vegetable# #tea# #cake# and #tea# so on."
我想计算句子
s
中受
#
约束的单词的频率

我想要以下输出

[("meat with vegetable", 1)
 ("tea", 2)
 ("cake", 1)]

非常感谢你的帮助和时间

使用
re
计数器的功能,可以轻松完成此任务:

In [1]: import re

In [2]: s = "& how are you then? I am fine, % and i want to found some food #meat with vegetable# #tea# #cake# and #tea# so on."

In [3]: re.findall(r'#([^#]*)#', s)
Out[3]: ['meat with vegetable', 'tea', 'cake', 'tea']

In [4]: from collections import Counter

In [5]: Counter(re.findall(r'#([^#]*)#', s))
Out[5]: Counter({'tea': 2, 'cake': 1, 'meat with vegetable': 1})

通过阅读和上的文档获取更多信息。

请考虑我对您文章的修订!谢谢你的修改你自己试过什么吗?这不是一个代码编写服务['meat with Plane'、'tea'、'cake'、'tea']计数器({'tea':2,'cake':1,'meat with Plane':1})这现在对应于'piglei's的建议,我得到了以下结果,['meat with Plane'、'tea'、'cake'、'tea']计数器({'tea':2,'cake':1,'meat with Plane':1})我是python新手,你的意思是使用re.find()和re.count()?请给我更多的点击量,我对pythonThanks很陌生,我仔细阅读了你的帖子,现在我明白了。谢谢我得到了一个txt文件,里面有很多不同的句子,你知道如何导入为s。谢谢你的点击,现在我解决了我的问题。