How to disable Python program from printing same value in list more than once?

时间:2015-10-06 08:48:46

标签: python

So I have a program that randomly selects a certain amount of items from a list, but if a value is randomly selected I would like it to only print once, and make the program unable to print the same value twice. How do I do this?

import random
list = [25, 50, 75, 100]
big = int(raw_input("Big numbers: "))
y = big
b = 0
while b < y:
    j = random.choice(list)
    print j
    b += 1

1 个答案:

答案 0 :(得分:1)

random.choice isn't the right method to use here. Use random.sample instead:

import random
mylist = [25, 50, 75, 100]
big = int(raw_input("Big numbers: "))
for num in random.sample(mylist, big):
    print num