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

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

Choisissez un index aléatoire dans la liste Python

import random  # Don't forget to install it first with pip install random

ahahfunny = ['ahah ', 'copy-', 'paste ', 'stack overflow',' go ', 'brrr']
#  the biggest index in this list above is 5 (0 being 1 in programming)

print(ahahfunny[random.randint(0, 5)]  # I like this way,
      
print(random.choice(ahahfunny)) # But this way is WAY thiccer...
stackoverflow.com

Sélectionnez la valeur aléatoire dans la liste Python

import random

# with replacement = same item CAN be chosen more than once.
# without replacement = same item CANNOT be chosen more then once.

# Randomly select 2 elements from list without replacement and return a list
random.sample(list_name, 2)

# Randomly select 3 elements from list with replacement and return a list
random.choices(set_name, k=3)

# Returns 1 random element from list
random.choice(list_name)
CodeHelper

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

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

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

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code