当应用于字符串和列表之间时,for循环不会产生具体的答案

2024-10-02 04:17:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从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的值多次出错。你知道吗


Tags: in标记列表forcountval单词elif
2条回答

我支持巴斯·伊尔沙德,规范化你的输入。以及您的参考数据(见下文)。此外,一个dictsetsdict索引可能是对于您的用例来说绝对是一个更好的数据结构

known_adj = {+4: {'legendary', 'legend', 'finest', 'insane', 'best'},
             +3: {'favorite', 'favourite', 'fav', 'delicious', 'awesome',
                  'perfect', 'perfection', 'perfectly', 'scrumptous'},
             ... }

total_val = sum(val for val in known_adj for adj in adjectives
                             if adj.strip().lower() in known_adj[val])

如果在匹配后跳过进一步的比较,for循环会更有效(编辑:,还提供了一种简单的方法来计算OP程序在循环期间累积的匹配总数,我刚刚忘记了这个细节…)

total_val = 0
# added in edit
total_matches = 0
for adj in adjectives:
    adj = adj.strip().lower()
    for val in known_adj:
        if adj in known_adj[val]:
             total_val += val
             # added in edit
             total_matches += 1
             continue

你可能想做的另一件事是消毒

 from itertools import combinations
 ...
 known_adj = update_ka()
 for i, j in combinations(known_adj.keys(),2):
     if known_adj[i].intersection(known_adj[j]):
         # not an empty set, there is a repetition!
         # print/log a warning, stop the machines, etc, you decide

使用collections模块


>>> from collections import Counter
>>> # Tally occurrences of words in a list
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
...     cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})

参考书目:
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

相关问题 更多 >

    热门问题