代码:
import random
x = ['A','B','C','D','E','F',
'G','H','I','J','K','L',
'M','N','O','P','Q','R',
'S','T','U','V','W','X',
'Y','Z']
y1 = random.sample(x, 2)
y2 = random.sample(x, 2)
y3 = random.sample(x, 2)
y4 = random.sample(x, 2)
y5 = random.sample(x, 2)
查询
如上所示,我选择了5个随机样本组合,并在变量y'x'
下声明它们。
为了改进我的代码,我想这样做,但要确保列表中的项目在所有变量输出中不会出现多次,其中所有组合都是不同且不重复的。我最好希望实现这一点,而不必从列表中删除项目,因为它将在代码中稍后重用。
预期输出(示例):
>>> y1
['A', 'Q']
>>> y2
['E', 'K']
>>> y3
['C', 'O']
>>> y4
['Z', 'X']
>>> y5
['P', 'L']
答案 0 :(得分:4)
你可以随机播放列表的副本(你说你想重复使用它,因此需要复制,因为shuffle就地工作)然后只为每个样本取两个元素:
import random
x_copy = x[:] # copy
random.shuffle(x_copy)
y1 = x[:2]
y2 = x[2:4]
y3 = x[4:6]
y4 = x[6:8]
y5 = x[8:10]
或者如果您不想对yi
s进行硬编码:
x_copy = x[:] # copy
random.shuffle(x_copy)
y = [x_copy[i*2: (i+1)*2] for i in range(5)]
print(y)
# [['W', 'Z'], ['A', 'Q'], ['B', 'J'], ['O', 'D'], ['X', 'E']]
答案 1 :(得分:2)
您可以使用numpy.random.choice
。它的目的是从类似数组的对象(也适用于您的列表)中选择(replace=True
)或不选择(replace=False
):
import numpy as np
x = ['A','B','C','D','E','F',
'G','H','I','J','K','L',
'M','N','O','P','Q','R',
'S','T','U','V','W','X',
'Y','Z']
np.random.choice(x, size=(5, 2), replace=False)
结果:
array([['Y', 'Q'],
['W', 'R'],
['O', 'H'],
['Z', 'G'],
['L', 'M']],
dtype='<U1')
这将返回一个包含5行的数组,每行包含一个大小为2的样本。
答案 2 :(得分:1)
您可以简单地构建生成值的“缓存” - 因此不会删除x
的元素:
import random
class SampleCache():
x = ['A','B','C','D','E','F',
'G','H','I','J','K','L',
'M','N','O','P','Q','R',
'S','T','U','V','W','X',
'Y','Z']
def __init__(self):
self.cache = []
def get(self):
_iterations = 0
while 1:
sample = random.sample(self.x, 2)
if not sample in self.cache:
self.cache.append(sample)
return sample
if _iterations > 1000: # just to prevent NOT to run into an infinite loop
break
s = SampleCache()
for x in range(25):
print(s.get())
答案 3 :(得分:1)
random.sample
是正确的方法,你需要用10个字母调用一次而不是用2个字母调用5次:
import random
import string
def random_letters(m=5, n=2):
letters = random.sample(string.ascii_uppercase, m * n)
return [letters[n * i:n * (i + 1)] for i in range(m)]
print(random_letters())
# [['I', 'X'], ['J', 'U'], ['O', 'W'], ['G', 'C'], ['D', 'F']]
print(random_letters())
# [['J', 'X'], ['N', 'P'], ['A', 'C'], ['O', 'Z'], ['B', 'H']]
print(random_letters())
# [['U', 'T'], ['J', 'N'], ['C', 'H'], ['D', 'I'], ['K', 'P']]
print(random_letters())
# [['U', 'G'], ['L', 'V'], ['A', 'R'], ['J', 'F'], ['S', 'C']]
print(random_letters())
# [['Y', 'C'], ['R', 'B'], ['E', 'I'], ['S', 'T'], ['H', 'X']]
答案 4 :(得分:0)
使用<div class="background" style="background-image: url('link-1-bkg.jpg');">
<a href="#" data-bkgimg="https://rlv.zcache.com/crash_test_dummy_marker_classic_round_sticker-r8b36e2d5c0f74a4c843565523094b867_v9wth_8byvr_324.jpg" class="is-active">Link 1</a>
<a href="#" data-bkgimg="https://dummyimage.com/300.png/09f/fff">Link 2</a>
</div>
生成初始列表的混洗副本,并使用生成器根据需要生成混洗值。
random.sample
输出;
def random_sample(x, n):
shuffled = random.sample(x, k=len(x))
for val in range(0, len(x), n):
yield shuffled[val: val+n]
print([sample for sample in random_sample(x, 2)])
如果你想要五个随机值,那么使用它;
[['I', 'O'], ['V', 'T'], ['U', 'J'], ['L', 'A'],
['E', 'G'], ['Q', 'F'], ['M', 'H'], ['B', 'K'],
['R', 'P'], ['W', 'N'], ['D', 'S'], ['Z', 'Y'],
['X', 'C']]
如果一次想要一个,那么使用,
samples = random_sample(x, 2)
five_samples = [next(samples) for _ in range(5)]
print(five_samples)
答案 5 :(得分:-1)
您可以循环生成的示例并从x
中删除元素:
x = ['A','B','C','D','E','F',
'G','H','I','J','K','L',
'M','N','O','P','Q','R',
'S','T','U','V','W','X',
'Y','Z']
new_x = x[:]
import random
final_list = []
for i in range(5):
the_sample = random.sample(new_x, 2)
final_list.append(the_sample)
for b in the_sample:
new_x.remove(b)
输出:
[['C', 'R'], ['L', 'V'], ['W', 'Y'], ['D', 'O'], ['J', 'Q']]