我想在整个程序中多次打印同一列表中的随机字符串,但不重复任何先前打印的随机字符串。
如果我有以下情况:
core = 'a', 'b', 'c', 'd'
print (random.sample(core[0:], k=2))
print (random.sample(core[0:], k=2))
我希望结果看起来像这样:
b, d
c, a
random.sample
本身无需替换即可工作,因此不会重复。获取4的样本并切片:
randoms = random.sample(core, 4)
print(randoms[:2])
print(randoms[2:])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句