Update 2:
Let's say that we have an answers
list.
# 1. Let's find out the pages:
pages = set([])
for a in answers:
pages.add(a.question.page)
pages = list(pages)
# 2. Now for each page,
for p in pages:
# 2.a. find out the answers that belong to this page
same_page_answers = [a for a in answers if a.question.page = p]
# 2.b. find out the questions of these answers
same_page_questions = set([])
for a in same_page_answers:
same_page_questions.add(a.question)
same_page_questions = list(same_page_questions)
# 3. now we will check to see if each question appears at least twice
for q in same_page_questions:
same_page_question_answers = [a for a in same_page_answers if a.question=q]
if len(same_page_question_answer) < 2:
# Ok so the same_page_question_answers need to be removed from our initial answers
answers = [a for a in answers if a not in same_page_question_answer]
# actually if we are here same_page_question_answer will be an array with 1 element but in any case
Using your example
answers = [A1: Q1P1, A2: Q1P1, A3: Q1P2, A4: Q2P2]
pages = [P1, P2]
inside for loop for P1:
same_page_answers = [A1, A2]
same_page_questions = [Q1, Q1]
inside for loop for Q1:
len == 2 - we won't remove anything
inside for loop for P2:
same_page_answers = [A3, A4]
same_page_questions = [Q1, Q2]
inside for loop for Q1:
same_page_question_answers = [A3]
len = 1 - we will remove [A3] from answers
inside for loop for Q2:
same_page_question_answers = [A4]
len = 1 - we will remove [A4] from answers
answers = [A1,A2]
Hope it's working now :)