python – 使用假设进行随机抽样

在假设中,有一个
corresponding sampled_from() strategy
random.choice()

In [1]: from hypothesis import find, strategies as st

In [2]: find(st.sampled_from(('ST', 'LT', 'TG', 'CT')), lambda x: True)
Out[2]: 'ST'

但是,有没有办法让类似random.sample()的策略从序列中产生长度为N的子序列?

In [3]: import random

In [4]: random.sample(('ST', 'LT', 'TG', 'CT'), 2)
Out[4]: ['CT', 'TG']

最佳答案 你可以这样做:

permutations(elements).map(lambda x: x[:n])
点赞