I have taken a count of something and it came out to N.
Now I would like to have a list, containing 1 to N numbers in it.
Example:
N = 5
then, count_list = [1, 2, 3, 4, 5]
Also, once I have created the list, I would like to randomly select a number from that list and use that number.
After that I would like to select another number from the remaining numbers of the list (N-1) and then use that also.
This goes on it the list is empty.
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)
.
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
.
Create the list (edited):
count_list = range(1, N+1)
Select random element:
import random
random.choice(count_list)
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
.
Maintain a set
and remove a randomly picked-up element (with choice
) until the list is empty:
s = set(range(1, 6))
import random
while len(s) > 0:
s.remove(random.choice(list(s)))
print(s)
Three runs give three different answers:
>>>
set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([4])
set([])
>>>
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])
set([2])
set([])
>>>
set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])
set([1])
set([])
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 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'
Source: Stackoverflow.com