【问题标题】:Using Python to compare lists?使用 Python 比较列表?
【发布时间】:2017-04-24 09:36:08
【问题描述】:

我对 python 还很陌生,本学期才开始学习这门课程。我正在努力寻找一种编写代码的方法,该代码将正确答案存储为列表,然后从 txt 文件中读取 20 个问题中每个问题的学生答案,并将答案存储在另一个列表中。之后,我想比较列表,然后打印他们的答案,程序将显示一条消息,指示学生是否通过,(15 或更大的正确是通过)和总数正确和总数不正确。所以例如正确的答案为 A,C,A,A,D,B,C,A,C,B,A,D,C,A,D,C,B,B,D,A。对于学生的答案将只是创建您的自己的文本文件进行测试。任何帮助将不胜感激我目前的格式似乎不起作用,如下所示。

def main():

total = 0
index = 0
answers = [ 'A', 'C', 'A', 'A', 'D',\
            'B', 'C', 'A', 'C', 'B',\
            'A', 'D', 'C', 'A', 'D',\
            'C', 'B', 'B', 'D', 'A']

student_answers = open('student_solution.txt', 'r')

for answer in student_answers:
    print(answer.strip())

    while index in answers == student_answers:
        if student_answers[0] == answers[0]:
            total +=1
        else:
            total +=0



student_answers.close()
print('Total correct answers: ', total)
print('Total of incorrect answers: ', 20 - total)

if total >= 15:
    print('Congratulations! You passed the exam.')
else:
    print('Sorry, you have failed the exam.')

main()

这是似乎仍然存在问题的更新程序。我正在使用的学生答案是 A C A A D B C A C B A D C A D C B B D A C A A D B C A C B A D C A D C B B D D

def main():

total = 0
index = 0
answers = [ 'A', 'C', 'A', 'A', 'D',\
            'B', 'C', 'A', 'C', 'B',\
            'A', 'D', 'C', 'A', 'D',\
            'C', 'B', 'B', 'D', 'A']

infile = open('student_solution.txt', 'r')

student_answers = infile.readline()
infile.close()
print(student_answers)

for answer in student_answers:
    for y in range(len(answer)):
        if answer[y] == answers[y]:
            total += 1


print('Total correct answers: ', total)
print('Total of incorrect answers: ', 20 - total)

if total >= 15:
        print('Congratulations! You passed the exam.')
else:
        print('Sorry, you have failed the exam.')

main()

【问题讨论】:

  • while index in answers == student_answers: 这不是你认为的那样
  • 有问题吗?

标签: python list


【解决方案1】:

你可以通过这种方式计算total 压缩两个列表

total = 0
for stdnt_ans,correct_ans in zip(student_answers, answers):
    if stdnt_ans == correct_ans:
        total += 1

它比以这种更紧凑但更慢的方式递增 total 快 2 倍以上:

total += int(stdnt_ans == correct_ans)

【讨论】:

    【解决方案2】:

    这将得到您的总数。只需要调整你循环的方式。

    def main():
    
        total = 0
        index = 0
        answers = [ 'A', 'C', 'A', 'A', 'D',\
                    'B', 'C', 'A', 'C', 'B',\
                    'A', 'D', 'C', 'A', 'D',\
                    'C', 'B', 'B', 'D', 'A']
    
        student_answers = [[ 'A', 'C', 'A', 'A', 'D',\
                    'B', 'C', 'D', 'C', 'B',\
                    'A', 'D', 'C', 'A', 'D',\
                    'C', 'B', 'A', 'D', 'A'],\
                    [ 'A', 'C', 'D', 'D', 'D',\
                    'A', 'C', 'A', 'D', 'B',\
                    'D', 'D', 'C', 'A', 'D',\
                    'B', 'B', 'B', 'D', 'A']]
    
        for answer in student_answers:
            for i in range(len(answer)):
                if answer[i] == answers[i]:
                    total += 1
            print('Total correct answers: ', total)
            print('Total of incorrect answers: ', 20 - total)
            if total >= 15:
                print('Congratulations! You passed the exam.')
            else:
                print('Sorry, you have failed the exam.')
            total = 0
    
    main()
    

    【讨论】:

    • 我还删除了您的文件方面,只是为了创建要使用的内容。您可以将 student_answers 替换回原来的样子。
    • 我试过你的方法,它似乎有帮助,我现在唯一的问题是当我为我创建的 .txt 文件执行 readlines 时,这只是通过保存记事本并垂直和水平发布答案,并且只需复制教授作为测试提供的答案并粘贴到记事本中即可完成。当尝试将其水平粘贴在那里时,它说范围已达到最大值...我猜它会将这些答案作为 1 个元素与 20 个元素?但我也通过使用 infile = open('student_solution.txt', 'r') 和 student_answers = infile.readlines() 改变了我的格式。
    • 那么你有两种方法可以解决这个问题。您可以将每个文件变成一个列表,也可以将每一行变成一个列表。这真的取决于整个设置。上面的代码只是为了让您了解如何处理列表比较。您可以单独创建列表,然后像上面那样比较它们,或者您可以将其作为内联比较进行,这意味着从其源中读取每个元素。这实际上取决于您获得的数据的结构。
    【解决方案3】:
    for index, value in enumerate(answers):
        total += int(value == student_answers[index])
    
     print('Pass' if total >= 15 else 'Fail')
    

    【讨论】:

    • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of 如何解决这个问题。没有这个,你的回答就没有多少教育价值了——记住你是在为未来的读者回答这个问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2018-08-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多