I'm a beginner in programming. I'm trying to compare two lists and save the differences in a third list.
However, I ran into two problems:
- I don't know what to put in range, since the length of the list is unknown
- The bigger problem is that I got an error 'function object not subscriptable'
so, two questions:
How would you copy values to a list with unknown len? and more importantly
How can I fix this code?
def wrong_input(input_list, mc_answers): print() wrong_answers=[] for i in range (5): while input_list[i]!=mc_answers[i]: wrong_answers.append(input_list[i]) i =1 return wrong_answers
And:
def main():
mc_answers=['A', 'C', 'A', 'A', 'D']
display_test()
print(user_input())
input_list=user_input()
print(wrong_input(input_list, mc_answers))
def user_input():
user_answers=[]
print()
for i in range (5):
user_answers.append(input('\nAnswer' str(i 1) ': '))
return user_answers
def wrong_input(input_list, mc_answers):
print()
w_answers=[]
for i in range (5):
while input_list[i]!=mc_answers[i]:
w_answers.append(input_list[i])
i =1
return w_answers
main()
mc_answers is [A, C, A, A, D] I'm trying to compare it to whatever the user input was. So if user_answers was [A, A, A, A, A,], w_answers would be [C, D]
CodePudding user response:
Do not try to come up with your own algorithm to determine the difference between two lists. You can determine the difference between two lists using a set
. A set is a built in data type in Python which is called using either the set()
function (similar to the list()
function), or by using {}
.
You can utilize the symmetric_difference
method to determine the difference between both lists, or you can use the difference
method to determine the difference in one list that isn't in another. Consider,
list1 = [3, 12, 7, 8]
list2 = [1, 5, 12, 3]
list3 = set(list1).symmetric_difference(list2)
print(list(list3))
This outputs:
[1, 5, 7, 8]
Which means that the numbers 1, 5, 7, and 8 are not common between both list1
and list2
. Depending on what you desire, you could use the difference
method:
list1 = [3, 12, 7, 8]
list2 = [1, 5, 12, 3]
list3 = set(list1).difference(list2)
print(list(list3))
This outputs:
[7, 8]
This means that 7 and 8 are in list1
but are not in list2
. You need to keep in mind some consequences with sets, however. For instance, If you convert a list to a set, it will remove duplicates. See this:
list1 = [1, 1, 2, 3]
print(set(list1))
Which outputs:
{1, 2, 3}
For a better explanation of sets, see this.
Edit
If all you want is a function that accomplishes the example that you have given us, then sets are probably not the way to go because you are dealing with lists that have duplicates. You can figure out what goes into the range
function using the len
function.
def wrong_input(input_list, correct_answers):
wrong_answers = []
for i in range(len(correct_answers)):
for j in range(len(input_list)):
# Move to the next item in correct_answers if this is true:
if input_list[j] == correct_answers[i]:
break
# If we hit this condition, then that means that we looped through all of input_list and there were no
# items that were the equal an individual item in correct_answers
if j == len(input_list) - 1:
wrong_answers.append(correct_answers[i])
return wrong_answers
mc_answers = ['A', 'C', 'A', 'A', 'D']
user_answers = ['A', 'A', 'A', 'A', 'A']
print(wrong_input(user_answers, mc_answers))
This outputs:
['C', 'D']