Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing two lists
#1
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?
Reply
#2
You can use built-in function zip()
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
We haven't covered zip() in class yet so it can not be used.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
How so? So I can avoid them in the future.
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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.
Reply
#8
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?
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing 2 lists BerryK 4 4,543 Apr-15-2017, 09:46 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020