2016-05-16 4 views
0

Ich versuche, ein Programm zu schreiben, das eine Liste von Fragen für einen Test ausgibt. Was ich versuche, ist, zu vermeiden, dass Duplikate auf diese Weise zu der Liste hinzugefügt werden, wenn ich die Liste drucke. Ich habe nur eine bestimmte Anzahl einzigartiger Elemente.Wie kann ich vermeiden, doppelte Elemente einer Liste in Python zuzuordnen?

def pick_questions(input_list, number_of_picks): 
    """Picks random elements of an input list given the number of picks""" 
    selected_strings = [] 

    for index in range(0, number_of_picks + 1): 
     random_index = randint(0, len(input_list) - 1) 

     if input_list[random_index] not in selected_strings: 
      selected_strings.append(input_list[random_index]) 
      random_index = randint(0, len(input_list) - 1) 

    return selected_strings 
+4

Verwenden Sie [set] (https://docs.python.org/2/library/stdtypes.html#set). –

+1

@kanayamalakar, sollte _not_ nicht eingerückt werden – Holloway

+1

'für index in range (0, number_of_picks + 1):' ist nicht wollen Sie wollen, es gibt Ihnen die Zahlen '0, 1, ..., npicks' das ist, werden Sie habe 'npicks + 1' Extraktionen aus deiner Eingabeliste. – gboffi

Antwort

0

Initiieren Sie Ihre Liste als Set. Set kann nur eindeutige Werte enthalten. Nachdem Sie Ihre Arbeit getan haben, ändern Sie Ihren Satz zurück in die Liste.

set = {1, 2, 3} 
>>> set 
set([1, 2, 3]) 
>>> set.add(4) # this would add 4 to the set because the set does not have 4 
>>> set 
set([1, 2, 3, 4]) 
>>> set.add(4) # this would *not* add 4 to the set because the set already has 4 
>>> set 
set([1, 2, 3, 4]) 
>>> list(set) 
[1, 2, 3, 4] 

entnehmen Sie bitte für weitere Informationen zu this link.

+0

Sie verlieren jede Bestellung, wenn Sie Elemente auf diese Weise speichern. – Holloway

+1

wie @Holloway sagte, dies würde die Reihenfolge verlieren. Wenn Sie die Bestellung beibehalten müssen, beziehen Sie sich bitte auf diesen Post. http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-reserving-order –

5

Sie random.sample nutzen könnten so gäbe es keine Filterung tun müssen, werden:

>>> import random 
>>> random.sample(range(10), 5) 
[1, 4, 3, 8, 7] 
0

Wenn, wie es scheint, können Sie das random Modul verwenden, hat random eine sehr komfortable Funktion für Ihren Anwendungsfall

from random import sample as pick_questions 

sample ‚s Dokumentation von yhe ipython Prompt

In [4]: sample? 
Signature: sample(population, k) 
Docstring: 
Chooses k unique random elements from a population sequence or set. 

Returns a new list containing elements from the population while 
leaving the original population unchanged. The resulting list is 
in selection order so that all sub-slices will also be valid random 
samples. This allows raffle winners (the sample) to be partitioned 
into grand prize and second place winners (the subslices). 

Members of the population need not be hashable or unique. If the 
population contains repeats, then each occurrence is a possible 
selection in the sample. 

To choose a sample in a range of integers, use range as an argument. 
This is especially fast and space efficient for sampling from a 
large population: sample(range(10000000), 60) 
File:  ~/src/miniconda3/lib/python3.5/random.py 
Type:  method 
Verwandte Themen