开发者

python random.sample stop sampling after all possible outcomes

开发者 https://www.devze.com 2023-03-27 04:25 出处:网络
I am using random.sample to sampleall possible combinations of sets of data (about 150 sets). The sample sizes I will be testing are 3,4 and 5 and the sets of data range between 2 and 20 items.

I am using random.sample to sample all possible combinations of sets of data (about 150 sets). The sample sizes I will be testing are 3,4 and 5 and the sets of data range between 2 and 20 items.

Each data point will be a string e.g. '101A'. I was going to just loop the random sampling 1000 times and store the points as a ordered concatenated string to cancel out duplicates. e.g.

d['2-101a-124'] = 0 

Then to extract the data then split the data by '-'s. Is there a better way of doing this? Limiting the number of times it randomly samples to obtaining all combinations?

edit: Just for clarification I need all possible combinations of a list i.e.

dataset = ['1','2','3A','4']

when sampling 3 data points I need al开发者_Python百科l combination, as in:

combination 1 = ['1','2','3A']
combination 2 = ['2','3A','4']
combination 3 = ['1','3A','4']
combination 4 = ['1','2','4']


With not use standard library?

>>> import itertools
>>> dataset = ['1','2','3A','4']
>>> list(itertools.combinations(dataset, 3))
[('1', '2', '3A'), ('1', '2', '4'), ('1', '3A', '4'), ('2', '3A', '4')]


If you can enumerate all combinations and put them in a list

a = [ list of all combinations ]

You can then shuffle it to put them in a random order

random.shuffle(a)

That way you'll have exactly 1 of each from the original. I'm not 100% sure I follow the goal though so maybe this is not what you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号