get a sample of 5 elements from an array in python

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.py
import random

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sample = random.sample(my_array, 5)

print(sample)
109 chars
7 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