We help IT Professionals succeed at work.
Private
Troubleshooting Question

Creating permutations by randomly selecting values from a list

Europa MacDonald
on
63 Views
Last Modified: 2020-08-05
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
Comment
Watch Question

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
I'm not sure whether I should answer this.. cause this was and is one of my test questions in coachings and seminars..

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

Commented:
There is the module itertools and the function permutations inside. It is part of the standard distribution of Python; so, there is no need to install any other package.

The following code just prints the generated lists from the list [1, ..., 100] generated by the range(1, 101) function. However, you can use whatever input list. You probably want to break the program using Ctrl+C as it produces a lot of permutations:
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)

Open in new window


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 random module and its sample generator, and wrap it to, say, tuple. Like this:
import random

pool = list(range(1, 101))
#print(pool)

for n in range(5):
    print(tuple(random.sample(pool, 5)))

Open in new window

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)

Open in new window


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)

Open in new window

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.

Open in new window

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
Let's start by clarifying your needs:

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.
Europa MacDonaldChief slayer of dragons

Author

Commented:
Ste5an I apologise, I see the error in how I have asked that question.
(I have also edited the original question for future clarity)

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 ?
Research Engineer - Electrical
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
OK, to add... Getting the sequence of the five elements could be done like that:
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

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

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
Empower Your Career
Did You Know?

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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

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.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.