我正试着识别一个特定的单词,然后数一数。我需要保存每个标识符的计数。
例如,
无风险利率 星号风险 市场风险[风险]
*文件内有以上字句,我需要计算“风险”而非星号。我还需要把风险算作‘风险’。这是我到目前为止所拥有的。但是,它返回星号和[风险和风险]的计数。我不需要清点星号,只需要计算包括风险在内的风险。我试着使用正则表达式,但仍然会出现错误。另外,我是Python的初学者。如果有人有任何想法,请帮助我!
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]发布于 2012-08-31 13:47:59
再来一次。匹配由单词边界包围的字符串'risk'
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发布于 2012-08-31 13:43:46
采用流水线的方法。我的意思是,在将单词添加到字典之前,对文本执行任何转换,以便计数是正确的。
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']只要你数过你的“风险”这个词,它计数“星号”这个词就行了。
发布于 2012-08-31 13:48:26
你可以试试这个片段:
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_counthttps://stackoverflow.com/questions/12216474
复制相似问题