“Sélectionnez un élément d'une liste par hasard” Réponses codées

Comment obtenir un élément aléatoire d'un tableau de Python

import random
names=['Mark', 'Sam', 'Henry']

#Set any array
random_array_item=random.choice(names)

#Declare a variable as a random choice
print(random_array_item)

#Print the random choice
#Or, if you want to arrange them in any order:
for j in range(names):
  print(random.choice(names))
Ugliest Unicorn

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 un élément d'une liste par hasard

import random 

rand_index = random.randrange(len(list)) 
random_num = list[rand_index] 

random_num = random.choice(list)
JJSSEECC

Réponses similaires à “Sélectionnez un élément d'une liste par hasard”

Questions similaires à “Sélectionnez un élément d'une liste par hasard”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code