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

Python - retrieve a specific sentence from a large text file

这是我的句子:

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)]

非常感谢您的帮助和时间!

利用reCounter的力量,这个任务可以轻松完成:

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})

阅读 python re and collections.Counter 上的文档获取更多信息。