列表中的互斥随机抽样。蟒蛇

时间:2013-04-19 17:23:38

标签: python function random sample mutual-exclusion

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

我无法在不重复任何元素的情况下创建X个大小为Y的列表

我一直在做的是:

x = 3
y = 2

import random

output = random.sample(input, y)

>['mordor','thingol']

但是,如果我重复这一点,那么我将重复

我希望输出类似于

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

因为我选择了大小为y = 2(每个列表2个元素)的x = 3(3个列表)

def random_generator(x,y):
    ....

3 个答案:

答案 0 :(得分:2)

您可以简单地随机播放原始列表,然后从中连续生成nm个元素组。可能存在少于或多于该组的数量。请注意input是Python内置函数的名称,因此我将其重命名为words

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))

输出:

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

迭代生成组会更加Pythonic。上面的函数可以很容易地转换为生成器,方法是逐个使yield每个组,而不是在相对更长的列表列表中将它们全部返回:

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)])

答案 1 :(得分:1)

您可以将random.sampleitertools.grouper食谱结合使用。

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)

一次运行,结果如下:

[('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')]

下一次运行:

[('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')]

答案 2 :(得分:1)

而不是从列表中随机抽取两件事,只需随机化您的列表并迭代它以创建您指定的维度的新数组!

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)