简体   繁体   English

如何检查答案是否在2D列表中

[英]How do I check if an answer is in a 2D list

blueprint = [[1,"A"], [2,"C"], [3,"B"], [4,"D"], [5,"A"], [6,"A"], [7,"B"], [8,"A"], [9,"C"], [10,"A"], [11,"D"], [12,"A"], [13,"C"], [14,"C"]
         ,[15,"B"], [16,"A"], [17,"B"], [18,"A"], [19,"C"], [20,"D"]]


def check_answer(students_answer):
    points = 0
    total = 20
    if students_answer[0]==blueprint[0][1]:
        points =+ 1
    elif students_answer[1]==blueprint[1][1]:
        points =+ 1
#etc.

    else:
        points =+ 0
    score = (points*100)/(total)
    return score

print(check_answer(["A", "C"]))

Why doesn't my function calculate the % of right answers for longer than answer 1, which is "A"? 为什么我的函数无法计算出正确答案的百分比长于答案1(即“ A”)的时间?

I don't get why you need to recall the list index in blueprint but if the data are like this, you could just zip the data together, unpack, and compare. 我不知道为什么你需要召回列表中的索引blueprint但如果数据是这样的,你可以只zip的数据一起,解压缩,并进行比较。

This way of doing it is really unefficient. 这种方式确实没有效率。 What if you have 100 questions? 如果您有100个问题怎么办? will you copy/paste 100 times your (wrong) elif statements? 您是否会复制/粘贴100次错误的elif语句?

Score 1 for success, 0 for fail, divide by total and multiply by 100, in a generator comprehension fed to sum : 生成器理解为成功,得分为1,失败为0,除以总数并乘以100,得出的sum

def check_answer(students_answer):
    return 100*(sum(correct==attempt for (_,correct),attempt in zip(blueprint,students_answer))/len(blueprint))

The first items in the tuples in your blueprint list are redundant because they are simply the indices of their respective tuples plus one. blueprint列表中元组的第一项是多余的,因为它们只是它们各自元组的索引加一个。 You should make it a simple list of strings instead: 您应该改为使用简单的字符串列表:

blueprint = ['A', 'C', 'B', 'D', 'A', 'A', 'B', 'A', 'C', 'A', 'D', 'A', 'C', 'C', 'B', 'A', 'B', 'A', 'C', 'D']

so that you can calculate the percentage of right answers in student_answer with a zip of the two lists like this: 这样您就可以使用两个列表的邮政编码来计算student_answer中正确答案的百分比:

sum(1 for s, b in zip(student_answer, blueprint) if s == b) / len(student_answer) * 100

Why doesn't my function calculate the % of right answers for longer than answer 1, which is "A"? 为什么我的函数无法计算出正确答案的百分比长于答案1(即“ A”)的时间?

Let's take a look at your data & function 让我们来看看您的数据和功能

# data
blueprint = [[1,"A"], [2,"C"], [3,"B"], [4,"D"], [5,"A"], [6,"A"], [7,"B"], [8,"A"], [9,"C"], [10,"A"], [11,"D"], [12,"A"], [13,"C"], [14,"C"], [15,"B"], [16,"A"], [17,"B"], [18,"A"], [19,"C"], [20,"D"]]

# function call
print(check_answer(["A", "C"]))

first list in blueprint is [1,"A"] . blueprint第一个列表是[1,"A"] the check_answer() argument is ["A", "C"] . check_answer()参数为["A", "C"] When the line in the loop here runs, it found what it's looking for 当循环中的行运行时,它找到了要查找的内容

if students_answer[0]==blueprint[0][1]:    # students_answer[0] is "A", blueprint[0][1] is "A", too
    points =+ 1
elif students_answer[1]==blueprint[1][1]:
    points =+ 1
#etc.

that means it skips all the elifs & else, straight to calculating score . 这意味着它将跳过所有省略号,否则将直接计算出score That's why score only contains the first "A" . 这就是为什么score只包含第一个"A"

one approach you can do is to use zip(*blueprint) to unpack the lists inside blueprint, into this 您可以使用的一种方法是使用zip(*blueprint)zip(*blueprint)的列表解压缩,

[(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
('A', 'C', 'B', 'D', 'A', 'A', 'B', 'A', 'C', 'A', 'D', 'A', 'C', 'C', 'B', 'A', 'B', 'A', 'C', 'D')]

then loop on that to count the grades. 然后循环计算分数。

so, 所以,

scorelist = list(zip(*blueprint))
total = len(scorelist[1])
answer_list = ["A", "C"]
first_a, second_a = answer_list
points = 0

for sc in scorelist[1]:
    if sc == first_a or sc == second_a:
        points += 1

score = (points*100)/(total)
print(score)

or you can even just joined all the scores strings & use str.count() 或者您甚至可以加入所有乐谱字符串并使用str.count()

joined_scores = ''.join(scorelist[1])
print(joined_scores)
# 'ACBDAABACADACCBABACD'

points = joined_scores.count(first_a) + joined_scores.count(second_a)
print(points*100/total)
# 65.0

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

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