首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何搜索、计数和保存单词?

如何搜索、计数和保存单词?
EN

Stack Overflow用户
提问于 2012-08-31 13:34:36
回答 5查看 113关注 0票数 0

我正试着识别一个特定的单词,然后数一数。我需要保存每个标识符的计数。

例如,

无风险利率 星号风险 市场风险[风险]

*文件内有以上字句,我需要计算“风险”而非星号。我还需要把风险算作‘风险’。这是我到目前为止所拥有的。但是,它返回星号和[风险和风险]的计数。我不需要清点星号,只需要计算包括风险在内的风险。我试着使用正则表达式,但仍然会出现错误。另外,我是Python的初学者。如果有人有任何想法,请帮助我!

代码语言:javascript
复制
from collections import defaultdict
word_dict = defaultdict(int)

for line in mylist:
words = line.lower().split()
for word in words:
    word_dict[word]+=1

for word in word_dict:
if 'risk' in word:
    word, word_dict[word]
EN

回答 5

Stack Overflow用户

发布于 2012-08-31 13:47:59

再来一次。匹配由单词边界包围的字符串'risk'

代码语言:javascript
复制
import re
re.findall(r'\brisk\b', 'risk risk') ## 2 matches
re.findall(r'\brisk\b', 'risk risk riskrisk') ## 2 matches
re.findall(r'\brisk\b', 'risk risk riskrisk [risk') ## 3 matches
re.findall(r'\brisk\b', 'risk risk riskrisk [risk asterisk') ## 3 matches
票数 2
EN

Stack Overflow用户

发布于 2012-08-31 13:43:46

采用流水线的方法。我的意思是,在将单词添加到字典之前,对文本执行任何转换,以便计数是正确的。

代码语言:javascript
复制
word_dict = {} # empty dictionary

for line in mylist:
    words = line.strip().lower().split() # the strip gets rid of new lines
    for word in words:
        # the strip here will strip away any surrounding punctuation.
        # add any other symbols to the string that you need
        # the key insight here, is you get rid of extra stuff BEFORE inserting
        # into the dictionary
        word_dict[word.strip('[/@#$%')]+=1 

for word in word_dict:
    print word, word_dict[word]

# to just see the count for risk:
print word_dict['risk']

只要你数过你的“风险”这个词,它计数“星号”这个词就行了。

票数 1
EN

Stack Overflow用户

发布于 2012-08-31 13:48:26

你可以试试这个片段:

代码语言:javascript
复制
import shlex

words = shlex.split("risk risk risk free interest rate")
word_count = len([word for word in words if word == "risk" or word =="[risk"])
print word_count
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12216474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档