简体   繁体   English

如何比较空元素的列表元素?

[英]How to compare list elements for empty element?

The first input array is the key to the correct answers to an exam, like ["a", "a", "b", "d"].第一个输入数组是考试正确答案的关键,例如 ["a", "a", "b", "d"]。 The second one contains a student's submitted answers.第二个包含学生提交的答案。

The two lists are not empty and are of the same length.这两个列表不为空且长度相同。 Return the score for this list of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer, represented as an empty string (in C the space character is used).返回此答案列表的分数,每个正确答案为 +4,每个错误答案为 -1,每个空白答案为 +0,表示为空字符串(在 C 中使用空格字符)。 If the score < 0, return 0.如果分数 < 0,则返回 0。

For example:例如:

checkExam(["a", "a", "b", "b"], ["a", "c", "b", "d"]) → 6
checkExam(["a", "a", "c", "b"], ["a", "a", "b",  ""]) → 7

My solution: it is working fine for all other conditions:我的解决方案:它适用于所有其他条件:

def checkExam(arr1,arr2):
    total = 0
    for i,j in zip(arr1, arr2):
        if i == j:
            total += 4
        if i != j:
            total -= 1
        if total < 0:
            return 0
    return total

** Execept this one: ** 执行这个:

checkExam(["a", "a", "c", "b"], ["a", "a", "b",  ""]) → 7

The problem is that I don't know how to compare ( "b" ) with ( " " ) (empty element) so it return +0 for blank answer.问题是我不知道如何将 ( "b" ) 与 ( " " ) (空元素) 进行比较,所以它返回 +0 作为空白答案。

** **

try this i think it solves your issue..let me know if it doesn't.试试这个,我认为它可以解决您的问题..如果没有,请告诉我。 even i new to this so any help with code improvement will be great即使我是新手,所以对代码改进的任何帮助都会很棒

def check_exam(arr1,arr2):
    total = 0
    for i,j in zip(arr1, arr2):
        if i == j:
            total += 4
        elif j=="": #i have made a change here
            total += 0
        elif i != j:
            total -= 1
    return max(total,0)

Why do you return from inside the loop when you reach a negative total?为什么当你达到负总数时你从循环内部返回? Just apply the logic as given:只需应用给定的逻辑:

def check_exam(arr1, arr2):
    total = 0
    for i, j in zip(arr1, arr2):
        if i == j:
            total += 4
        elif j:  # only deduct if j is non-emtpy
            total -= 1  
    return max(total, 0)  # no negative marks

>>> check_exam(["a", "a", "b", "b"], ["a", "c", "b", "d"])
6
>>> check_exam(["a", "a", "c", "b"], ["a", "a", "b",  ""])
7

And if you really want a cryptic one-liner:如果你真的想要一个神秘的单线:

def check_exam(arr1, arr2):
    return max(0, sum(4*(i==j) or -bool(j) for i, j in zip(arr1, arr2)))
    # or less cryptic
    return max(0, sum(4 if i==j else -1 for i, j in zip(arr1, arr2) if j))

You could also do it as you would in real life.你也可以像在现实生活中那样做。 You know, on paper.你知道,在纸上。 Where you wouldn't compare a not-given answer to the expected answer at all.您根本不会将未给出的答案与预期答案进行比较。 Where you'd skip that entirely.你会完全跳过的地方。

def check_exam(expect, given):
    total = 0
    for e, g in zip(expect, given):
        if g:
            total += 4 if e == g else -1
    return max(0, total)

One-liner version:单行版本:

def check_exam(expect, given):
    return max(0, sum(4 if e == g else -1 for e, g in zip(expect, given)))

Other one-liners:其他单行:

def check_exam(expect, given):
    return max(0, sum((-1, 4)[e == g] for e, g in zip(expect, given)))
def check_exam(expect, given):
    return max(0, sum(f'    {e}'.find(g) for e, g in zip(expect, given)))

The latter one would also handle ' ' correctly (well, like '' ):后一个也可以正确处理' ' (嗯,就像'' ):

>>> check_exam(['a', 'a', 'a', 'a'], ['a', 'b', '', ' '])
3

This will give you what you want这会给你你想要的

def check_exam(solution, student_submit): 
  scores = [0 if y == "" else 4 if (y==x) else -1 for (x,y) in zip(solution,student_submit)]
  return max(sum(scores),0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM