sample() Method to Randomly Select Elements From a List. Python has a built-in function called random. sample(). The random module contains the random.
You can use random. randint() and random. randrange() to generate the random numbers, but it can repeat the numbers.
In order to get a random item from a List instance, you need to generate a random index number and then fetch an item by this generated index number using List. get() method. The key point here is to remember that you mustn't use an index that exceeds your List's size.
You can create the enumeration of the elements by something like this:
mylist = list(xrange(10))
Then you can use the random.choice
function to select your items:
import random
...
random.choice(mylist)
As Asim Ihsan correctly stated, my answer did not address the full problem of the OP. To remove the values from the list, simply list.remove()
can be called:
import random
...
value = random.choice(mylist)
mylist.remove(value)
As takataka pointed out, the xrange
builtin function was renamed to range
in Python 3.
You can try this code
import random
N = 5
count_list = range(1,N+1)
random.shuffle(count_list)
while count_list:
value = count_list.pop()
# do whatever you want with 'value'
As for the first part:
>>> N = 5
>>> count_list = [i+1 for i in xrange(N)]
>>> count_list
[1, 2, 3, 4, 5]
>>>
As for the second, read 9.6. random — Generate pseudo-random numbers.
>>> from random import choice
>>> a = choice(count_list)
>>> a
1
>>> count_list.remove(a)
>>> count_list
[2, 3, 4, 5]
That's the general idea.
By the way, you may also be interested in reading Random selection of elements in a list, with no repeats (Python recipe).
There are a few implementations of fast random selection.
You don't need to count stuff if you want to pick a random element. Just use random.choice()
and pass your iterable:
import random
items = ['foo', 'bar', 'baz']
print random.choice(items)
If you really have to count them, use random.randint(1, count+1)
.
You can use:
import random
random.choice(range(n))
or:
random.choice(range(1,n+1))
if you want it from 1
to n
and not from 0
.
After that I would like to select another number from the remaining numbers of the list (N-1) and then use that also.
Then you arguably do not really want to create a list of numbers from 1 to N just for the purpose of picking one (why not just ask for a random number in that range directly, instead of explicitly creating it to choose from?), but instead to shuffle such a list. Fortunately, the random
module has you covered for this, too: just use random.shuffle
.
Of course, if you have a huge list of numbers and you only want to draw a few, then it certainly makes sense to draw each using random.choice
and remove it.
But... why do you want to select numbers from a range, that corresponds to the count of some items? Are you going to use the number to select one of the items? Don't do that; that's going out of your way to make things too complicated. If you want to select one of the items, then do so directly - again with random.choice
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With