Mona Jalal
Mona Jalal

Reputation: 38235

random.shuffle(points).take(k) in Python

What is the equivalent of this Scala line in Python?

 random.shuffle(points).take(k)

I don't seem to find the take method for shuffle in Python

Upvotes: 0

Views: 143

Answers (2)

Sanyam Mehra
Sanyam Mehra

Reputation: 309

This is one option you have, from numpy, to randomly shuffle points

np.random.shuffle()

This is one option you have, from numpy, to randomly choose k out of points

np.random.choice(points, size=k)

Upvotes: 0

Oleg Pyzhcov
Oleg Pyzhcov

Reputation: 7353

You can pick k random elements out of an iterable using sample

import random

return random.sample(points, k)

Also, random has shuffle (but it is mutating) and you can use slices instead of take:

copy = points[:]
random.shuffle(copy)
return copy[:k]

Upvotes: 3

Related Questions