3
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']]

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

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

3 回答 3

2

您可以简单地打乱原始列表,然后从中连续生成元素n组。m可能有可能少于或多于该数量的组。请注意,这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)])
于 2013-04-19T19:13:30.377 回答
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)
于 2013-04-19T18:25:14.953 回答
1

您可以与食谱random.sample结合使用。itertools.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')]
于 2013-04-19T17:29:16.303 回答