Python Forum
comparing two lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: comparing two lists (/thread-15554.html)

Pages: 1 2


comparing two lists - Siylo - Jan-21-2019

I am trying to compare two lists. The first list is a constant of "correct answers". The second list is the "answers from a student". How would I check each index of the students answers list against the same index from the correct answers list? I would imagine having to use a for loop for this?


RE: comparing two lists - perfringo - Jan-21-2019

You can use built-in function zip()


RE: comparing two lists - Siylo - Jan-21-2019

We haven't covered zip() in class yet so it can not be used.


RE: comparing two lists - ichabod801 - Jan-21-2019

You will need to loop through the possible indexes of the lists, using a for loop, range, and len. Then compare the lists at that index.

You should know that this is the wrong way to program Python. Your teacher is requiring you to use poor programming techniques.


RE: comparing two lists - Siylo - Jan-21-2019

How so? So I can avoid them in the future.


RE: comparing two lists - ichabod801 - Jan-21-2019

They are making you use indexes when you don't need to. It needlessly complicates your code. The more complicated your code is the harder it is to maintain and debug.

What perfingo says about zip is right. What you want to do should be done this way:

for item_a, item_b in zip(list_a, list_b):
    if item_a == item_b:
        # do whatever.
But since they haven't taught you zip yet, you have to do it with indexes, which is more complicated and error prone.

It's like the thing I showed you in the other thread about random.choice. They require you to generate a random index and then access that index. But random.choice does the same thing without that complication.


RE: comparing two lists - nilamo - Jan-21-2019

(Jan-21-2019, 07:31 PM)Siylo Wrote: We haven't covered zip() in class yet so it can not be used.

We're not going to write the code for you. Give it a try, show us what you've done, and we'll help guide you to a working solution.


RE: comparing two lists - Siylo - Jan-21-2019

It is very tough on me because I like to search out the best or most efficient way to do something but in this class we are not allowed/rewarded for doing so. She seems to think if she didn't teach it to us then there is no way for us to know how to use it. I commented out the read file to empty list part of the program and copied the info to the list to make it easier for help.

CORRECT_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" , "A" ,
                   "B" , "C" , "A" , "C" , "C" ,
                   "A" , "D" , "C" , "A" , "A" ,
                   "C" , "B" , "B" , "D" , "D"]

#student_file = open("test.txt" , "r")


#for line in student_file:
#    student_answers.append(line.rstrip('\n'))


#student_file.close()


correct_answers = 0

for index in CORRECT_ANSWERS , student_answers:
    if CORRECT_ANSWERS[0] == student_answers[0]:
        correct_answers += 1

print(correct_answers)
I am getting an output of 2 and it should be 16 for the correct_answers. What am I missing here?


RE: comparing two lists - ichabod801 - Jan-21-2019

You are looping over this tuple: (CORRECT_ANSWERS, student_answers). So that's two items, which is why you get two. Note that your comparison on line 25 is always comparing the first item from each list.

You need to loop over range(len(CORRECT_ANSWERS)). Then use your loop variable (index) to index the correct items in the two lists.


RE: comparing two lists - Siylo - Jan-21-2019

CORRECT_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" , "A" ,
                   "B" , "C" , "A" , "C" , "C" ,
                   "A" , "D" , "C" , "A" , "A" ,
                   "C" , "B" , "B" , "D" , "D"]

#student_file = open("test.txt" , "r")


#for line in student_file:
#    student_answers.append(line.rstrip('\n'))


#student_file.close()


correct_answers = 0
index_position = 0
for index in range(len(CORRECT_ANSWERS)):

    if CORRECT_ANSWERS[index_position] == student_answers[index_position]:
        correct_answers += 1
    index_position += 1
print(correct_answers)
This works.

I still have more to do with it but I should be able to figure it out. The for loop condition was what was really confusing me. Thank you. These things happen when trying to work ahead of the class strictly from a book.