Private

Troubleshooting Question

Is it possible in python to create x number of 5 value permutations from a list of 100 values ?

using each number only once (until all the numbers were exhausted or until there were less than 5 ? )

so if the list was 1..100

to create

35 87 12 94 54

34 19 76 50 32

More detail to this question

ok so we have the values 1 to 20

the question is, how to randomly choose values in groups of 5 until there are no values left and choosing each number only once.

so, choosing

3 18 7 12 15 would eliminate those values from being chosen again, leaving 15 numbers to choose from

1 2 4 5 6 8 9 10 11 13 14 16 17 19 20

Then to make the next permutation, or group of numbers in the same random way leaving only 10 numbers to choose from and so on until none are left

Is this possible, and if so how would I do it ?

my apology also to pepr , I was not clear in the original question

using each number only once (until all the numbers were exhausted or until there were less than 5 ? )

so if the list was 1..100

to create

35 87 12 94 54

34 19 76 50 32

More detail to this question

ok so we have the values 1 to 20

the question is, how to randomly choose values in groups of 5 until there are no values left and choosing each number only once.

so, choosing

3 18 7 12 15 would eliminate those values from being chosen again, leaving 15 numbers to choose from

1 2 4 5 6 8 9 10 11 13 14 16 17 19 20

Then to make the next permutation, or group of numbers in the same random way leaving only 10 numbers to choose from and so on until none are left

Is this possible, and if so how would I do it ?

my apology also to pepr , I was not clear in the original question

Can you tell us about the problem and the reason for the question?

The following code just prints the generated lists from the list

```
import itertools
##print(list(range(1, 101))) # you can uncomment to see the generated list
for lst in itertools.permutations(range(1, 101), 5):
print(lst)
```

Anyway, the generator is rather systematic, the results are not randomized. The problem with randomized results is that you need to remember the values that were already generated. So, you should add what exactly is the purpose.

For random samples (here without checking whether the sample does not repeat), you can use the standard

```
import random
pool = list(range(1, 101))
#print(pool)
for n in range(5):
print(tuple(random.sample(pool, 5)))
```

You get the results like this```
(16, 18, 69, 70, 83)
(36, 29, 22, 42, 27)
(66, 35, 93, 25, 84)
(5, 20, 70, 80, 48)
(74, 49, 63, 28, 60)
```

If you need only "few" (really meaning not extremely many), you can remember the already generated tuples in a set. Here the pool was restricted to few to show how it behaves:

```
import random
pool = list(range(1, 6))
#print(pool)
observed = set()
for n in range(100):
result = tuple(random.sample(pool, 5))
while result in observed:
print(f'\t{result} again...')
result = tuple(random.sample(pool, 5))
observed.add(result)
print(result)
```

You should observe something like...```
e:\__Python\EuropaMacDonald\ee29190546>py c.py
(4, 2, 3, 1, 5)
(4, 5, 3, 2, 1)
(2, 4, 3, 1, 5)
(5, 2, 4, 1, 3)
(1, 4, 3, 5, 2)
(3, 5, 2, 1, 4)
(2, 3, 4, 5, 1)
(4, 1, 2, 5, 3)
(5, 2, 1, 3, 4)
(1, 2, 3, 4, 5)
(4, 5, 3, 1, 2)
(4, 1, 2, 5, 3) again...
(3, 4, 1, 2, 5)
(2, 4, 3, 5, 1)
(4, 5, 1, 3, 2)
(1, 3, 4, 2, 5)
(5, 1, 4, 2, 3)
(1, 2, 4, 5, 3)
(2, 1, 5, 4, 3)
(4, 1, 3, 5, 2)
(1, 2, 3, 5, 4)
(5, 3, 4, 1, 2)
(1, 5, 4, 3, 2)
(4, 1, 2, 3, 5)
(2, 4, 5, 3, 1)
(1, 5, 2, 4, 3)
(4, 2, 1, 5, 3)
(4, 2, 5, 3, 1)
(3, 2, 5, 4, 1)
(4, 2, 5, 3, 1) again...
(4, 1, 3, 2, 5)
(3, 4, 5, 1, 2)
(4, 2, 5, 1, 3)
(1, 5, 4, 2, 3)
(5, 2, 3, 4, 1)
(4, 3, 1, 5, 2)
(1, 3, 5, 2, 4)
(2, 4, 5, 3, 1) again...
(2, 4, 1, 3, 5)
(2, 3, 1, 5, 4)
(5, 4, 1, 2, 3)
(3, 4, 5, 2, 1)
(3, 2, 5, 4, 1) again...
(4, 5, 2, 1, 3)
(2, 1, 3, 4, 5)
(5, 1, 2, 4, 3)
(1, 4, 2, 5, 3)
(5, 2, 4, 3, 1)
(3, 1, 5, 4, 2)
(1, 3, 5, 2, 4) again...
(4, 3, 2, 5, 1)
(5, 1, 4, 3, 2)
(4, 3, 5, 1, 2)
(1, 3, 2, 5, 4)
(4, 2, 1, 5, 3) again...
(3, 2, 5, 1, 4)
(2, 3, 5, 1, 4)
(1, 4, 2, 5, 3) again...
(2, 5, 3, 4, 1)
(1, 3, 5, 2, 4) again...
(4, 2, 5, 3, 1) again...
(2, 3, 1, 4, 5)
(5, 4, 3, 1, 2)
(5, 3, 4, 2, 1)
(4, 1, 3, 5, 2) again...
(2, 3, 1, 4, 5) again...
(4, 1, 3, 2, 5) again...
(1, 2, 5, 3, 4)
(5, 2, 1, 3, 4) again...
(1, 4, 2, 5, 3) again...
(5, 2, 3, 4, 1) again...
(4, 2, 1, 5, 3) again...
(3, 1, 5, 2, 4)
(2, 3, 5, 1, 4) again...
(1, 5, 4, 3, 2) again...
(1, 3, 4, 5, 2)
(1, 2, 4, 5, 3) again...
(4, 1, 3, 2, 5) again...
(3, 1, 5, 2, 4) again...
(4, 1, 5, 3, 2)
(3, 4, 5, 1, 2) again...
(1, 2, 3, 5, 4) again...
(4, 1, 3, 2, 5) again...
(2, 1, 4, 3, 5)
(2, 1, 5, 4, 3) again...
(4, 3, 2, 5, 1) again...
(3, 5, 2, 1, 4) again...
(2, 5, 4, 3, 1)
etc.
```

Do you need combinations (k-combinations over S), where order does not matter. E.g. 2 from 3 {a,b,c} => {a,b}, {a,c}, {b,c}.

Or permutations (precisely k-permutation of n), where order matters. E.g. 2 frm 3 {a,b,c} => (a,b), (a,c), (b,a), (b,c), (c,a), (c b).

The problem is 5 of 100 are 100! / (100-5)! possible permutations (~9 billion (10^9)). Calculating them and drawing a random one is pretty inefficient. So I'm not sure whether this is the correct approach.

This one is on us!

(Get your first solution completely free - no credit card required)

UNLOCK SOLUTION
```
import random
lst = list(range(1, 101)) # the odered pool of values
random.shuffle(lst) # shuffled randomly
while len(lst) > 0: # while there is anything to get
print(lst[0:5]) # get the first five of them
del lst[0:5] # remove the first five of them
```

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy

Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif

Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster

CTP, Sr Infrastructure Consultant

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

- Troubleshooting
- Research
- Professional Opinions

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.