我有三个值列表(数字和字母),我想编写一个程序来随机组合每个列表中的一个。
我找到了一个代码,可以生成所有可能的值组合,我认为这可能是一个很好的基础,但现在我不知道如何继续。谁能帮我?
这是我的代码
import itertools
square = [a, s, d, f, g, h, j, k, l ]
circle = [w, e, r, t, z, u, i, o, p ]
line = [y, x, c, v, b, n, m ]
radiusshape = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
for L in range(0, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print(subset)
Run Code Online (Sandbox Code Playgroud)
您可以使用从生成的笛卡尔积中random.sample抽取k随机样本
# where k is number of samples to generate
samples = random.sample(itertools.product(square, circle, line, radiusshape), k)
Run Code Online (Sandbox Code Playgroud)
例如
>>> a = [1, 2, 3, 4]
>>> b = ['a', 'b', 'c', 'd']
>>> c = ['foo', 'bar']
>>> random.sample(set(itertools.product(a,b,c)), 5)
[(1, 'c', 'foo'),
(4, 'c', 'bar'),
(1, 'd', 'bar'),
(2, 'a', 'foo'),
(2, 'd', 'foo')]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3899 次 |
| 最近记录: |