Search code examples
pythonnumpypython-itertools

How to sample without duplication in python?


I want to sample 30 samples in a list with length 100.

I can use np.random.sample like this:

import numpy as np
l = list(range(100))
np.random.choice(l, 30)
# np.random.choice(l, 30) may be same with next time np.random.choice(l, 30)

This can get a sample group, but I need to sample several times.

For example, I need 3 sublist(length=3) from a list with length 10(just a demo).

the duplicated samples is meaningless for me. which means [1,2,3] and [3,2,1] is duplicated for me. but np.random.choice may produce there two for me.

Is there any methods in python that can help me? Or is there a way I can compare the sampled list faster?


Solution

  • If your list has only 100 elements, you can random.shuffle it and get the first 30 elements.

    import random
    
    l = list(range(100))
    random.shuffle(l)
    print(l[:30])