Question

input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']

I'm having trouble creating X number of lists of size Y without repeating any elements.

What I have been doing is using:

x = 3
y = 2

import random

output = random.sample(input, y)
# ['mordor', 'thingol']

but if I repeat this then I will have repeats.

I would like the output to be something like

[['mordor', 'thingol'], ['melian', 'hithlum'], ['beleriand', 'eol']]

since I chose x = 3 (3 lists) of size y = 2 (2 elements per list).

def random_generator(x,y):
    ....
Was it helpful?

Solution 2

rather than randomly taking two things from your list, just randomize your list and iterate through it to create your new array of the dimensions you specify!

import random
my_input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']
def random_generator(array,x,y):
    random.shuffle(array)
    result = []
    count = 0
    while count < x:
        section = []
        y1 = y * count
        y2 = y * (count + 1)
        for i in range (y1,y2):
            section.append(array[i])
        result.append(section)
        count += 1
    return result
print random_generator(my_input,3,2)

OTHER TIPS

You can simply shuffle the original list and then generate n groups of m elements successively from it. There may be fewer or more than that number of groups possible. Note thatinputis the name of a Python built-in function, so I renamed itwords.

import itertools
from pprint import pprint
import random

def random_generator(seq, n, m):
    rand_seq = seq[:]  # make a copy to avoid changing input argument
    random.shuffle(rand_seq)
    lists = []
    limit = n-1
    for i,group in enumerate(itertools.izip(*([iter(rand_seq)]*m))):
        lists.append(group)
        if i == limit: break  # have enough
    return lists

words = ['beleriand', 'mordor', 'hithlum', 'eol', 'morgoth', 'melian', 'thingol']
pprint(random_generator(words, 3, 2))

Output:

[('mordor', 'hithlum'), ('thingol', 'melian'), ('morgoth', 'beleriand')]

It would be more Pythonic to generate the groups iteratively. The above function could easily be turned into generator by making ityieldeach group, one-by-one, instead of returning them all in a relatively much longer list-of-lists:

def random_generator_iterator(seq, n, m):
    rand_seq = seq[:]
    random.shuffle(rand_seq)
    limit = n-1
    for i,group in enumerate(itertools.izip(*([iter(rand_seq)]*m))):
        yield group
        if i == limit: break

pprint([group for group in random_generator_iterator(words, 3, 2)])

You could use random.sample in combination with the itertools.grouper recipe.

input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']
import itertools
import random
def grouper(iterable,group_size):
    return itertools.izip(*([iter(iterable)]*group_size))

def random_generator(x,y):
    k = x*y
    sample = random.sample(input,k)
    return list(grouper(sample,y))

print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)
print random_generator(3,2)

for one run, this results in:

[('melian', 'mordor'), ('hithlum', 'eol'), ('thingol', 'morgoth')]
[('hithlum', 'thingol'), ('mordor', 'beleriand'), ('morgoth', 'eol')]
[('morgoth', 'beleriand'), ('melian', 'thingol'), ('hithlum', 'mordor')]
[('beleriand', 'thingol'), ('melian', 'hithlum'), ('eol', 'morgoth')]
[('mordor', 'hithlum'), ('eol', 'beleriand'), ('melian', 'morgoth')]
[('mordor', 'melian'), ('thingol', 'beleriand'), ('morgoth', 'eol')]

And the next run:

[('mordor', 'thingol'), ('eol', 'hithlum'), ('melian', 'beleriand')]
[('eol', 'beleriand'), ('mordor', 'melian'), ('hithlum', 'thingol')]
[('hithlum', 'mordor'), ('thingol', 'morgoth'), ('melian', 'eol')]
[('morgoth', 'eol'), ('mordor', 'thingol'), ('melian', 'beleriand')]
[('melian', 'morgoth'), ('mordor', 'eol'), ('thingol', 'hithlum')]
[('mordor', 'morgoth'), ('hithlum', 'thingol'), ('eol', 'melian')]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top