【问题标题】:For loop works but is not summing in pythonFor循环有效,但不在python中求和
【发布时间】:2021-04-06 11:14:13
【问题描述】:

我的输入是一个单词列表。如["love", "sky", "NOT_movie"], ["NOT_house", "NOT_CAR"]。我想做的是,对于这个单词列表,如果单词以“NOT_”开头,我给它打-1,如果它不是以“NOT_”开头的单词,我给它打+1。我的预期结果是一个列表,其中包含我初始列表中每个集合的分数

我有以下代码,应该根据以下条件通过添加 +=1 来计算分数

 for i in range(len(temp2)):
        for j in range(len(temp2[i])):
            if temp2[i][j] in lex2:
                if "NOT_" in list_test_data2[i][j]:
                    value = lex2[str(temp2[i][j])] * (-1)
                    print(value)
                else: 
                    value = lex2[str(temp2[i][j])]
                    print(value)
                    
                    if value == 1 :
                        count_pos+=1
                    elif value == -1 :
                           count_neg+=1
            
        list_scores2.append((count_pos - count_neg)/(count_pos + count_neg + 2))
        count_pos=0
        count_neg=0

但是,当打印 list_scores2 我有以下输出

print(list_scores2)

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

我做错了什么?

【问题讨论】:

  • 你能告诉我们输入数据吗?还有你想用代码实现的目标(输出)
  • 压痕看起来坏了。您应该提供一个我们可以运行的minimal reproducible example。 IE。您应该提供一些硬编码数据供我们运行示例。
  • @VedankPande 我的输入是一个单词列表。如["love", "sky", "NOT_movie"], ["NOT_house", "NOT_CAR"]。我想做的是,对于这个单词列表,如果单词以“NOT_”开头,我给它打-1,如果它不是以“NOT_”开头的单词,我给它打+1。我的预期结果是一个列表,其中包含我初始列表中每个集合的分数。
  • 好的,那个列表是temp2? lex2list_test_data2 呢?
  • 样本输入/输出可能吗?

标签: python arrays for-loop


【解决方案1】:

您可以使用列表推导先循环遍历列表,然后再遍历每个列表。

word_lists = [["love", "sky", "NOT_movie"], ["NOT_house", "NOT_CAR"]]

[
    sum([-1 if word.startswith("NOT_") else 1 for word in word_list])
    for word_list in word_lists
]

【讨论】:

    【解决方案2】:

    这不是最pythonic或最有效的方法,但它有效:

    ip = [["love", "sky", "NOT_movie"], [ "NOT_house", "NOT_CAR"]]
    
    res = []
    
    for sublist in ip: #note how the list is iterated through - instead of using range()
        score_list = []  #this holds score for each word in a sublist
        for word in sublist:
            if word.startswith("NOT_"):
                score_list.append(-1)
            else:
                score_list.append(1)
        res.append(sum(score_list)) #append total score for that sublist into res list
    
    print(res)
    

    输出:

    [1,-2]
    

    【讨论】:

      【解决方案3】:

      一些想法。

      1. 首先,检查您的第一个 if 语句:if temp2[i][j] in lex2: 我不确定 lex2 中的内容,但如果该语句永远不正确,您可能会得到您所描述的输出。因此,首先检查代码是否曾经进入该 if 语句。如果不是,那么这就是问题所在。

      2. 如果if "NOT_" in list_test_data2[i][j]: 始终为真,那么它也会产生您所获得的输出。如果是这种情况,那么您的计数变量将永远不会改变,因为它只存在于 else 语句中。

      3. value 永远不会是 +1 或 -1。这意味着输出将始终为零。也许检查返回的值的类型。 “-1”与-1不同。

      要考虑的另一件事是,是否有其他方法来构建它,这可能有助于更有效地执行此操作和描述性,因此更容易调试。我在上面看到了一些不错的建议,但这里有一些提示:

      • 我会避免将变量命名为“temp”,因为它们会使您更难以理解您的代码在做什么。

      • 循环遍历单词而不是索引。如果您可以使用内置函数来使代码更小,那就更好了。可能有比这更好的版本,但我认为这可能与您所拥有的大致相同:

        for wordlist in temp2:
          count_pos = sum("NOT_" in word for word in wordlist)    
          count_neg = len(wordlist) - count_pos
          list_scores2.append((count_pos - count_neg)/(count_pos + count_neg + 2))
        

      【讨论】:

        【解决方案4】:

        我认为一个简单的班轮可能就可以解决问题

        >>>[sum(1 if word.startswith("NOT_") else -1 for word in subl) for subl in [["love", "sky", "NOT_movie"], ["NOT_house", "NOT_CAR"]]]
        
        [-1, 2]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-07
          • 2018-12-03
          • 1970-01-01
          • 2023-01-01
          • 1970-01-01
          • 2019-05-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多