“Python Choisissez un échantillon aléatoire dans la liste” Réponses codées

python choisissez l'élément aléatoire dans la liste

import random

#1.A single element
random.choice(list)

#2.Multiple elements with replacement
random.choices(list, k = 4)

#3.Multiple elements without replacement
random.sample(list, 4)
MitroGr

Comment imprimer une partie aléatoire d'une liste dans Python

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
Careful Caracal

Python Choisissez un échantillon aléatoire dans la liste

import random
sequence = [i for i in range(20)]
subset = random.sample(sequence, 5) #5 is the lenth of the sample
print(subset) # prints 5 random numbers from sequence (without replacement)
Kodi4444

Python Random Choice de la liste

import random
list = [20, 30, 40, 50 ,60, 70, 80]
sampling = random.choices(list, k=4)      # Choices with repetition
sampling = random.sample(list, k=4)       # Choices without repetition
Andrea Perlato

Réponses similaires à “Python Choisissez un échantillon aléatoire dans la liste”

Questions similaires à “Python Choisissez un échantillon aléatoire dans la liste”

Plus de réponses similaires à “Python Choisissez un échantillon aléatoire dans la liste” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code