Reputation: 38235
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
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
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