我有一个从nltk用pos\u标记和单词\u标记获得的形容词串。共有7个列表:
positiverange4 = ['legendary', 'legend', 'finest', 'insane', 'best']
positiverange3 = ['favorite', 'favourite', 'fav', 'delicious', 'awesome', 'perfect', 'perfection', 'perfectly', 'scrumptous']
positiverange2 = ['love', 'courteous', 'great', 'generous', 'tasty', 'pleasent', 'polite']
positiverange1 = ['like', 'enjoyable', 'enjoy', 'reasonable', 'huge', 'plentiful', 'plenty', 'quick', 'enjoyed', 'fast', 'swift']
neutralrange = ['ok', 'fine', 'good', 'nice', 'gud', 'friendly', 'fresh', 'cheap']
negativerange1 = ['crowded', 'lousy', 'slow', 'bad']
我启动一个for循环,检查该字符串中的一个单词是否在这些列表中,如果它退出,我按如下方式递增计数器
count = 0
for w in adjectives:
if w in positiverange4:
val += 4
count = count + 1
elif w in positiverange3:
val += 3
count = count + 1
elif w in positiverange2:
val += 2
count = count + 1
elif w in positiverange1:
val += 1
count = count + 1
elif w in neutralrange:
val += 0
count = count + 1
elif w in negativerange1:
val -= 1
count = count + 1
elif w in negativerange2:
val -= 2
count = count + 1
elif w in negativerange3:
val -= 3
count = count + 1
elif w in negativerange4:
val -= 4
count = count + 1
print count
count的值多次出错。你知道吗
我支持巴斯·伊尔沙德,规范化你的输入。以及您的参考数据(见下文)。此外,一个
dict
的set
s的对于您的用例来说绝对是一个更好的数据结构dict
索引可能是如果在匹配后跳过进一步的比较,
for
循环会更有效(编辑:,还提供了一种简单的方法来计算OP程序在循环期间累积的匹配总数,我刚刚忘记了这个细节…)你可能想做的另一件事是消毒
使用
collections
模块参考书目:
8.3. 集合-高性能容器数据类型-http://goo.gl/GGWYrW
9.7. itertools-为有效循环创建迭代器的函数-http://goo.gl/GKfVXQ
Python列表http://goo.gl/HZ9Hm
在线演示http://repl.it/4NP
联机执行Python脚本http://goo.gl/4sxrD
相关问题 更多 >
编程相关推荐