【问题标题】:for loop does not yield a concrete answer when applied between a string and lists当在字符串和列表之间应用时,for 循环不会产生具体的答案
【发布时间】:2014-11-17 07:04:32
【问题描述】:

我有一串形容词,是使用 nltk 中的 pos_tag 和 word_tokenize 获得的。有 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的值多次出错。

【问题讨论】:

  • “错误”是什么意思?它计算您的列表之一中adjectives 中的单词数。你能给出输入和预期输出吗?
  • adjectives 到底是什么?
  • 试试for w.strip().lower() in adjectives:
  • 形容词是使用 pos_tag 和 word_tokenize 获得的一串形容词。它有点像这样..['general', 'nice', 'easy', 'good', 'great', 'scrumptious' , '附属', '一流', '重要', '更多'] ['很多', '其他', '好', '可怕', '坏', '很多', '其他', '医疗', '难以理解', '遗憾'] ['高效', '伟大', '愉快', '友好', '少数', '权威', '高兴']
  • adjectives 是一个嵌套列表。

标签: python list loops for-loop iteration


【解决方案1】:

我支持 BATH IRSHAD,规范化您的意见。还有你的参考数据(见下文)。此外,sets 的 dictionary 可能是对于您的用例来说绝对是一个更好的数据结构

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

您可能想做的另一件事是清理known_adj

 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

【讨论】:

    【解决方案2】:

    使用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

    【讨论】:

    • 这里使用Counter 导致循环数据,填充 Counter,然后循环Counter 以计算分数. OTOH,如果 OP 的程序对唯一形容词的计数有其他用途...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 2023-01-24
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多