2017-09-03 68 views
-1

我有以下列表:存储索引引用(从文件读入)。使用索引而不是值来搜索时从列表中删除重复

easy_answers=[row[ca],row[1],row[2],row[3],row[4]] 

值的示例:

row[ca] = 1 #this is a flag that points to the right response, in this case row1 which is a 
row[1] = a 
row[2] = b 
row[3] = c 
and so on 

所有我想要做的就是简单检查清单,看看是否任何持有列表元素的索引是相同的(重复),如果因此,删除重复指数(但在行[CA]值离开,因为这是正确答案)

在上面的例子中,预期输出将是:

easy_answers=[row[ca],row[1],row[2],row[3],row[4]] 
#we know from the values that row[ca] = 1 and row[1] =1, so remove row[1] 

..所以最后的结果将是:

easy_answers=[row[ca],row[2],row[3],row[4]] 

代码

只值打交道时,从列表中删除重复的简单,但我怎么改下面的代码来搜索由索引号码所保存的值以实现相同的目的?

ca=int(row[5]) #this is the right resopnse for the question 
       easy_answers=[row[ca],row[1],row[2],row[3],row[4]] 

      if row[ca] in easy_answers: 

        easy_answers.remove(row[ca]) 

回答

1

而不是增加,然后重复数据删除,你可能会更好删除正确的答案,然后再重新添加它。

import random 

correct_answer = 2 
num_choices = 3 
possibles = [1, 2, 3, 4] 
# 
if len(possibles) != num_choices: 
    possibles.remove(correct_answer) 
    random.shuffle(possibles) 
    possibles = possibles[:num_choices - 1] 
    possibles.append(correct_answer) 
random.shuffle(possibles)