1

I have a list of over million tuples and want a list of 100,000 tuples randomly sampled from that original list without replacement. I have read:

How do I pick 2 random items from a Python set?

but the solution provided (using random.sample while casting list to a set) does not work for tuples.

I am using Python 2.7

Community
  • 1
  • 1
Marjoram
  • 413
  • 1
  • 7
  • 15

2 Answers2

3

Not sure if I fully understand your question, but random.sample can accept both lists and tuples:

random.sample([1,2,3,4,5],2)
Out[620]: [2, 5]

random.sample((1,2,3,4,5),2)
Out[621]: [4, 1]
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • I though random.sample sampled with replacement and that is why, in the link I provided in the question, they cast the list to a set – Marjoram Jun 02 '15 at 23:35
  • No it's sampling without replacement. – maxymoo Jun 02 '15 at 23:37
  • 1
    @Marjoram: No, they call `set` because the question is specifically about sampling from sets, and before the introduction of set literals, `set([a, b, c])` was the way to create a set of `a`, `b`, and `c`. – user2357112 Jun 02 '15 at 23:37
2

You can sample your list regardless of if the elements are hashable or not:

data = create_dataset() # List of data to be sampled

n_samples = 100000
samples = random.sample(data,n_samples)
Tommi Kerola
  • 146
  • 1
  • 7