To get a sample of 5 elements from an array in Python, you can use the random.sample()
function. This function takes two arguments: the array to sample from, and the number of elements you want to sample.
Here's an example:
main.py109 chars7 lines
In this example, we import the random
module and define an array called my_array
. We then use the random.sample()
function to sample 5 elements from my_array
, and store the result in a variable called sample
.
The output of this code will be a list of 5 random elements from my_array
. Note that the original array is unchanged by this operation.
gistlibby LogSnag